is_same将类模板实例化与其基类模板进行比较时返回 false

is_same returning false when comparing class template instantiation with its base class template?

本文关键字:比较 false 返回 基类 same is 实例化      更新时间:2023-10-16

>*编辑:不知何故,我认为编译器正在创建B就像A<int, int, string>一样,导致我对is_same应该如何评估它们的假设,无论继承/派生如何。 我的坏:( 对随后的误解表示歉意:\ *

制作一些元函数来检查我的自定义类型,并遇到了这个问题,但不确定我是否了解这里发生了什么。 我想我可以通过将已知类型的this_t成员与传递的任何参数this_t进行比较来解决它,但我只想了解为什么第 1 和第 3 is_same 测试失败:

template<typename... Args> struct A {
    typedef A<Args...> this_t;
};
struct B : A<int, int, string> {
};
//tests
std::is_same<A<int, int, string>, B>::value; //false
std::is_same<A<int, int, string>, typename B::this_t>::value; //true
std::is_same<B, typename B::this_t>::value; //false
//more tests for kicks
std::is_base_of<A<int, int, string>, B>::value; //true
std::is_base_of<A<int, int, string>, typename B::this_t>::value; //true
std::is_base_of<B, typename B::this_t>::value; //false

is_same是否通过A<...>基础进行区分? A<int, int, string>B之间的明显区别是什么?

is_same基本上是一个具有专业化的模板

template<class T, class U>
struct is_same : false_type
{ };
template<class T>
struct is_same<T, T> : true_type
{ };

这永远不会给你真实的,除非你有完全相同的类型。请注意,专业化中只有一个T。它永远无法同时匹配 A 和 B。

只有当传递给它的两种类型是完全相同的类型时,is_same特征才为真。 BA<int, int, string>的类型不同。

is_same测试两种类型是否相同。

BA<int, int, string>的类型不同。 怎么可能? 它源于它。