C++ 从对象转换<;常数T>;反对<;t>;

C++ 从对象转换<;常数T>;反对<;t>;,c++,casting,C++,Casting,假设我们有一个模板类: class Object<T> 如何调用函数: bool areEqual(const Object<IQuestion>& rhs, const Object<IQuestion>& rhs) const bool areEqual(常量对象和rhs,常量对象和rhs)常量 考虑到变量略有不同 我假设这可以通过static\u cast或reinterpret\u cast以某种方式实现。以下内容可能与您正在寻找

假设我们有一个模板类:

class Object<T>
如何调用函数:

bool areEqual(const Object<IQuestion>& rhs, const Object<IQuestion>& rhs) const
bool areEqual(常量对象和rhs,常量对象和rhs)常量
考虑到变量略有不同


我假设这可以通过
static\u cast
reinterpret\u 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 
}
模板
std::如果启用,则启用
areEqual(常量对象和左侧、常量对象和右侧){
//T和U是同一类型,撇开cv限定符和引用不谈
//如果你知道什么是比较它们的有意义的方法,那么就这样做
//注意这里T和U仍然可以有不同的cv限定符
}

请参阅上的最小工作示例。

Object
Object
是不同的、不相关的类,除非
X
Y
是完全相同的类型。您必须创建一个用于在两者之间进行转换的函数;或者干脆停止使用
对象
。我无法提供重载,也无法控制传入的参数。我想我可以克隆const变量并再构建一个Object实例。看看是否可以使用新的重载
bool areEqual(const Object&lhs,const Object&rhs)const
bool areEqual(const Object<IQuestion>& rhs, const Object<IQuestion>& rhs) const
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 
}