is_same将类模板实例化与其基类模板进行比较时返回 false
is_same returning false when comparing class template instantiation with its base class template?
>*编辑:不知何故,我认为编译器正在创建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
特征才为真。 B
与A<int, int, string>
的类型不同。
is_same
测试两种类型是否相同。
B
与A<int, int, string>
的类型不同。 怎么可能? 它源于它。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 伪造事实↔false
- QueryWorkingSet总是返回false
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++ - 负数和正数之间的比较返回 false
- 为什么将三个变量与 == 一起比较会计算为 false?
- 符合要求的编译器应该能够优化哪些指针比较以"always false"?
- 当参数相等时,为什么必须std :: sort比较函数返回false
- 比较两个具有相同值的 TCHAR 结果为 false
- 常量字符* if() 与 "SRAD" 的比较返回 false
- 与返回 false 的 fmod() 结果进行比较
- C++字符串比较返回0,但布尔值返回false
- C++比较运算符未返回true或false
- 为什么unsigned long和负数比较结果是false ?
- double比较类型的变量总是返回false
- is_same将类模板实例化与其基类模板进行比较时返回 false