Sign in to follow this  
jwein

typedef of a vector stl

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this