将类模板实例化与其基类模板进行比较时 is_same 返回 false?
*编辑:不知何故,我认为编译器正在创建 B
就像 A
一样,导致我假设 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<...> 进行区分code> base?A
和 B
之间有什么明显的区别?
*Edit: Somehow I thought the compiler was creating B
just as A<int, int, string>
, leading to my assumption about how is_same should evaluate them, regardless of inheritance/derivation. My bad :( Sorry for subsequent misunderstandings :\ *
Making some meta-functions to check for my custom types, and ran into this issue, but not sure I understand what's going on here. I think I can work around it by comparing this_t member of a known type to this_t of whatever parameter is passed, but I just want to understand why the 1st and 3rd is_same tests fail:
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 is_same differentiating by way of the A<...>
base? What's the appreciable difference between A<int, int, string>
and B
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
is_same 基本上是一个具有专门化的模板
,它永远不会给你 true ,除非你有完全相同相同的类型。请注意,专业化中只有一个
T
。它永远不能同时匹配 A 和 B。The is_same is basically a template with a specialization
That will never give you true, unless you have exactly the same type. Note that there is only one
T
in the specialization. It can never match both A and B.仅当传递给它的两个类型完全相同时,
is_same
特征才为 true。B
与A
的类型不同。The
is_same
trait is true only if the two types passed to it are the exact same type.B
is not the same type asA<int, int, string>
.is_same
测试两个类型是否是同一类型。B
与A
的类型不同。怎么可能呢?它是从它派生出来的。is_same
tests if two types are the same type.B
is not the same type asA<int, int, string>
. How could it be? It's derived from it.