开发者

g++ typedef templates in inheritor class

simplifying my problem we can consider:

template <class T>
class Base{
    typedef typename std::pair<T, T> pair;
};

template <class T>
class Inheritor : public Base<T> {
    pair *p;                          
    // mean that we want to use constructor of std::pair.
    // say: std::pair withou argument list

    Inheritor<T>::pair *p;
    // dont see his typename
    // say: pair does not name a type

    typename pair *p;
    // I was sure that it works.
    // I dont know why it doesnt work.
    // say: expected nested-开发者_高级运维name-specifier before 'pair

    typename Inheritor<T>::pair *p;
    // ok!
};

why we cant write typename pair *p ? I dont understand reasons of Inheritor:: ! it make code more complex and bad to read!

PS (of cource public. as I say "simplifying my problem...")

typedef typename Base<T>::pair pair;

In my mind it is a ... russian word that is hard to translate ("костыль")

It look like Kludge or duct tape or hack =)

As I understand:

typedefs inherit as usual function or variable. but it is not accessible (!!!). to acess it we should write

typedef typename Base<T>::pair pair;

or

typedef typename Inheritor<T>::pair pair;

it looks like funny Hindu code but we needs it! (>_<)''''

of cource in public scope


When a type name depends on a template parameter, it is a dependent name. You have to use typename to indicate you're naming a type. Read that article, and you'll see your use of typename doesn't make sense, except in the last case.

Here's how your code should probably look:

template <class T>
class Base
{
public: // you probably don't want a private typedef
    typedef std::pair<T, T> pair; // typename isn't needed here, this isn't dependent
};

template <class T>
class Inheritor : public Base<T>
{
public: // public again
    typedef typename Base<T>::pair pair; // get the Base's pair type, typedef it

    pair *p; // ah, easy to use
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜