1

*编辑:不知何故,我认为编译器正在创建Bas 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

4

3 回答 3

4

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。

于 2011-05-16T15:26:29.310 回答
3

is_same仅当传递给它的两种类型是完全相同的类型时,该特征才为真。B不是同一类型A<int, int, string>

于 2011-05-16T15:07:50.907 回答
2

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

B不是同一类型A<int, int, string>。怎么会这样?它源于它。

于 2011-05-16T15:07:45.340 回答