假设我们有一个模板类:
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_cast
或 reinterpret_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/