【发布时间】:2011-05-16 15:02:28
【问题描述】:
*编辑:不知何故,我认为编译器创建 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 之间有什么明显区别?
【问题讨论】:
-
我不明白你的编辑是什么意思,请解释一下。
-
我的意思是你的答案是在摇摆不定。如果在编译时遇到
B的使用错误,我会收到一条消息,将B称为A<int, int, string>,而不是B。因此,如果编译器将B视为A<int, int, string>,那么我不确定为什么 is_same 测试会失败。 -
还有。你期望什么输出?全部
true?还是全部false?还是什么? -
那么,您应该询问该错误。显示生成它的代码,并显示错误消息。
-
来吧伙计们。我是在寻求帮助,而不是赞助“叫我白痴”比赛。我试图了解发生了什么。如果编译器将
B称为A<int, int, string>,那么当我通过is_same 和A<int, int, string>并要求它与B进行比较时会给出什么?
标签: c++ templates c++11 template-meta-programming variadic-templates