开发者

How to access typedefs from inherited template

I have following code snippet:

template <typename T>
struct ChildStruct
{
  typedef std::map<int,T> Tmap;
};

struct DataStruct: public ChildStruct<long>
{

};

void Test()
{
  DataStruct::ChildStruct<long>::Tmap map;
}

It's possible to access a Tmap typedef located in the ChildStruct from outside of DataStruct without typedef-ing this ChildStruct inside of Datastruct?

When I use mentioned code snippet in Visual Studio, everything works OK, but linux/macos g++ give me error:

error: 'ChildStruct' is not a member of 'DataStruct'

I found a way by defining a helper typedef inside od DataStruct:

struct DataStruct: public ChildStruct<long>
{
    typedef ChildStruct<long> ChildStructLong;
};

void Test()
{
    DataStruct::ChildStructLong::Tmap map;
}

But I would preffer a way without ChildStructLong definition.

Thanks!

Edited:

The solution is call ChildStruct directly from outside of DataStruct as suggest Christian Rau. Sometime the simplest solution is the b开发者_Go百科est solution ;-)


Use the following:

typename Foo<double>::my_typedef blah;


Why don't you use DataStruct::Tmap directly?

#include <map>

template <typename T>
struct A
{
  typedef std::map<int, T> map_type;
};

struct B : A<int>
{ };

int main()
{
  B::map_type x;
}

See it working here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜