程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++ 强制转换模板类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决C++ 强制转换模板类型?

开发过程中遇到C++ 强制转换模板类型的问题如何解决?下面主要结合日常开发的经验,给出你关于C++ 强制转换模板类型的解决方法建议,希望对你解决C++ 强制转换模板类型有所启发或帮助;

虑以下代码片段:

[[nodiscard]] bool operator==(const BasicIterator<const Type>& rhs) const noexcept {
    if( this == &rhs ) {
        return true;
    }
    return node_ == rhs.node_;
}

[[nodiscard]] bool operator==(const BasicIterator<Type>& rhs) const noexcept {
    // how to call exisTing operator== implementation ??
    //return operator==( rhs );
}

我应该如何对 operator== 使用相同的实现?是否可以从 operator== <const Type> 调用 operator== <Type> 版本?

在这种情况下,模板类型是否有任何强制转换?

解决方法

template <typename T> [[nodiscard]] bool operator==(const BasicIterator<T>& rhs) const noexcept { if( this == &rhs ) { return true; } return node_ == rhs.node_; } 是模板吗?如果没有,也许把它做成一个模板?


private: 
template <typename T>
[[nodiscard]] bool operator==(const BasicIterator<T>& lhs,const BasicIterator<T>& rhs) const noexcept {
    if( &lhs== &rhs ) {
        return true;
    }
    return lhs.node_ == rhs.node_;
}

public:
[[nodiscard]] bool operator==(const BasicIterator<const Type>& rhs) const noexcept {
    return operator==<const Type>(*this,rhs);
}

[[nodiscard]] bool operator==(const BasicIterator<Type>& rhs) const noexcept {
    return operator==<Type>(*this,rhs);
}

如果您不想将此模板公开给用户,请将其隐藏在某处并在实现中使用:

{{1}}

大佬总结

以上是大佬教程为你收集整理的C++ 强制转换模板类型全部内容,希望文章能够帮你解决C++ 强制转换模板类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: