开发者

Ambiguous type reference

开发者 https://www.devze.com 2023-02-19 19:25 出处:网络
Why does this works : template <typename T> struct foo { }; struct A { typedef foo<A> type; }; struct B : public A

Why does this works :

template <typename T> 
struct foo
{
};

struct A
{
    typedef foo<A> type;
};

struct B : public A
{
    typedef foo<B> type;
};

int main()
{
    B::type john;
    return 0;
}

But not this :

template <typename T> 
struct foo
开发者_开发技巧{
};

template <typename T>
struct Shared
{
    typedef foo<T> type;
};

struct A : public Shared<A>
{
};

struct B : public A, public Shared<B>
{
};

int main()
{
    // g++ 4.5 says :
    // error: reference to 'type' is ambiguous
    B::type john;
    return 0;
}

In my code, foo is actually boost::shared_ptr and, as you can see, I'm trying so factor some typedefs using a Shared class.


Because B inherits foo<B> and, indirectly, foo<A>, and both contain a member type. Which did you mean?

Your simple, first piece of code has B's type hiding A's type, but that doesn't happen in the more complex second piece of code, which involves a deeper inheritance tree.


Because you actually have 2 type typedefs. One from A, which gets his from shared<A> and one from shared<B>. In your first case, you hide the type typedef of the A base class with the typedef in B.

0

精彩评论

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