从Object <const t =“”>转换为Object <t>

时间:2017-02-07 04:15:26

标签: 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以某种方式实现。

1 个答案:

答案 0 :(得分:1)

以下内容可能类似于您所寻找的内容:

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上查看最小的工作示例。

相关问题