开发者

is_same returning false when comparing class template instantiation with its base class template?

*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 :( Sor开发者_如何学Cry 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?


The is_same is basically a template with a specialization

template<class T, class U>
struct is_same : false_type
{ };

template<class T>
struct is_same<T, T> : true_type
{ };

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.


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 as A<int, int, string>.


is_same tests if two types are the same type.

B is not the same type as A<int, int, string>. How could it be? It's derived from it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜