gpt4 book ai didi

c++ - is_same 在将类模板实例化与其基类模板进行比较时返回 false?

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:47 26 4
gpt4 key购买 nike

*编辑:不知何故我认为编译器正在创建 B正如A<int, int, string> ,导致我假设 is_same 应该如何评估它们,而不管继承/派生。我的错 :( 对不起,随后的误解:\*

制作一些元函数来检查我的自定义类型,并遇到了这个问题,但我不确定我是否理解这里发生了什么。我想我可以通过将已知类型的 this_t 成员与传递的任何参数的 this_t 进行比较来解决它,但我只想了解为什么第一个和第三个 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。

关于c++ - is_same 在将类模板实例化与其基类模板进行比较时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com