c++ - 从 Object<const T> 转换为 Object<t>

标签 c++ casting

假设我们有一个模板类:

class Object<T>

还有一个变量实例:

Object<const IQuestion> qobj1(new Question());
Object<IQuestion> qobj2(new Question());

我想像这样调用函数 areEqual:

areEqual(question1, question2).

如何调用一个函数:

bool areEqual(const Object<IQuestion>& rhs, const Object<IQuestion>& rhs) const

考虑到变量略有不同?

我假设这可以通过 static_castreinterpret_cast 以某种方式实现。

最佳答案

以下内容可能与您正在寻找的内容类似:

template<typename T, typename U>
std::enable_if_t<std::is_same<std::decay_t<T>, std::decay_t<U>>::value, bool>
areEqual(const Object<T>& lhs, const Object<U>& rhs) {
    // T and U are the same type, put aside cv qualifiers and references
    // if you know what's a meaningful way to compare them, do that
    // note that here T and U can still have different cv-qualifiers 
}

请参阅 coliru 上的最小工作示例.

关于c++ - 从 Object<const T> 转换为 Object<t>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081616/

相关文章:

c++ - 为什么 const_cast 的行为不如预期?

c# - 重新解释将数组从字符串转换为整数

c++ - 在C++中为全局变量赋值

C++ 在 .txt 文件中查找整数值

c++ - 在 C++ 中使用 IFileDialog 以编程方式预选择

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

C++ 无法通过虚拟基 A 从基 A 转换为派生类型 B

mysql - 划分 COUNT 列时的 CAST 函数

c++ - C++ 中的 cast 属性

c++ - 为什么这里会出现字节顺序问题?