Jump to content
  • Advertisement
Sign in to follow this  
jwein

typedef of a vector stl

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm going to do a typedef of a std::vector class , but i don' t know how to do this... I' ve tried to do something similar: template < typename T, typename A > typedef std::vector < T, A > EArray. but it doesn't work... Can you help me ? Thx, bye :)

Share this post


Link to post
Share on other sites
Advertisement
typedef templates are not supported in C++. The closest you could get is:

template<typename T, typename A>
struct EArray
{
typedef std::vector<T,A> type;
};

// use EArray<foo,bar>::type instead of EArray<foo,bar>

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
typedef templates are not supported in C++. The closest you could get is:

template<typename T, typename A>
struct EArray
{
typedef std::vector<T,A> type;
};

// use EArray<foo,bar>::type instead of EArray<foo,bar>


yeah,for avoid the different meanings,i think
typedef typename std::vector< T,A > type; maybe better

Share this post


Link to post
Share on other sites
Quote:
Original post by jwein
Thanks !
typedef typename std::vector< T,A > type : doesn't work o.o


I think that was meant to replace the statement within the class, to avoid ambiguity. But this isn't a situation where that applies: 'std' is a *namespace*, so it's known that std::vector<T, A> must be a class type *for all* T, A.

Where there could be a problem is if you wanted to typedef, say, std::vector<T, A>::iterator, because it would be valid (syntactically - certainly it would be a serious WTF) for a specialization of std::vector to define a data member 'iterator' which overrides the typedef in the std::vector template. The 'typename' keyword says "assume this is a typename until you instantiate the template, and flag an error then if it isn't". Otherwise, the compiler is supposed to assume that it's a data member (*regardless of context*), and then the typedef-syntax is broken (because you specify a data member where a type-name is expected). This process actually helps the compiler give you more meaningful error messages.

Anyway, you *can* typedef an *instantiation* of the template: 'typedef std::vector<int, myallocator<int> > IntArray' works fine. But maybe that isn't useful to you?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!