When can I have my templated typedefs?

D

DeMarcus

I want to encapsulate the STL and today I use
this solution.

template< class T >
struct MyVector
{
typedef std::vector<T> Type;
};

And in use it becomes

MyVector<int>::Type someVector;

But I'm tired of using this ::Type extension.
Why can't I define MyVector like this?

template< class T >
typedef std::vector<T> MyVector;


Thanks
Daniel
 
S

Sharad Kala

DeMarcus said:
I want to encapsulate the STL and today I use
this solution.

template< class T >
struct MyVector
{
typedef std::vector<T> Type;
};

And in use it becomes

MyVector<int>::Type someVector;

But I'm tired of using this ::Type extension.
Why can't I define MyVector like this?

template< class T >
typedef std::vector<T> MyVector;

Because standard C++ as of now does not support template typedefs. In fact
you have addressed the problem just the way Herb Sutter describes in this
gotw article - http://gotw.ca/gotw/079.htm.
 
S

Sharad Kala

DeMarcus said:
But ain't the industry screaming for templated typedefs? What's the
drawback?

AFAIK, perhaps it will be added in the future revision of the standard.

-Sharad
 
T

tom_usenet

I want to encapsulate the STL and today I use
this solution.

template< class T >
struct MyVector
{
typedef std::vector<T> Type;
};

And in use it becomes

MyVector<int>::Type someVector;

But I'm tired of using this ::Type extension.
Why can't I define MyVector like this?

template< class T >
typedef std::vector<T> MyVector;

The concept of templated typedefs doesn't quite make sense, since a
typedef is an alias for a type, whereas MyVector above isn't a type
alias at all, but an alias for a family of types. The proposed syntax
looks like this:

template <class T>
using MyVector = std::vector<T>;

See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1489.pdf

Tom
 
B

Bob Hairgrove

Why can't I define MyVector like this?

template< class T >
typedef std::vector<T> MyVector;

The problem is that MyVector becomes an ambiguous name whenever there
are more than one vectors of different T types.

At one point I thought it might be useful, too ... but then I found
that there is probably no solution which involved less typing than
doing it the way you suggest, or by simply supplying an alias for each
different vector<T> type.

Guess I should read Herb Sutter's article...maybe I'm missing
something?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top