I think the real reason is that it simply won't allow for "template<typename T>" in a typedef, so you really can't use T as a type.
Otherwise it's right that you must specify a type. You can't pass a templated type without specifying a type.
Printable View
I think the real reason is that it simply won't allow for "template<typename T>" in a typedef, so you really can't use T as a type.
Otherwise it's right that you must specify a type. You can't pass a templated type without specifying a type.
I don't know why they did it, I only know it simply doesn't work.
And what the standard says, goes. That's life.
A typedef is an alias for a type. You can only create aliases for actual types. A template name is not a type (except within the context of its definition, but that's a special case that would complicate things now). "template <typename T> foo" is not a type. "foo<T>" is a type, if T is a type or a type parameter, therefore you can give it an alias.
Because it would make life so much nicer. Suppose I want a quick way to refer to the iterator type of ANY kind of std::vector<T>. What I want to do is this:
Then, I could use it like this:Code:template <typename T>
typedef std::vector<T>::iterator vec_iterator;
vec_iterator, being a "template typedef," does not name a type. But vec_iterator<int> does. Unfortunately, this construct is not supported yet. In practice, you do this:Code:std::vector<int> vec;
for(vec_iterator<int> i = vec.begin(), end = vec.end(); i != end; ++i)
{
...
}
And then instead of referring to vec_iterator<int>, you refer to vec_iterator<int>::type. Does this suck? Yes. You have to declare a struct whose only purpose is to contain this typedef. But it's what we have for the moment.Code:template <typename T>
struct vec_iterator
{
typedef typename std::vector<T>::iterator type;
};
Bottom line: you can achieve the same things as if we had template typedefs, it's just ugly.
Yay for C++0x, then.
Or something like that.Code:template <typename T>
using vec_iterator = std::vector<T>::iterator;
There were some sound reasons not to use typedef, but I can't remember them.