tags:

views:

92

answers:

3

Hello, is it possible in C++ to create an alias of template class (without specifying parameters)?

typedef std::map myOwnMap;

doesn't work.

And if not, is there any good reason?

+4  A: 

This feature will be introduced in C++0x, called template alias. It will be looking like this:

template<typename Key, typename Value>
using MyMap = std::map<Key, Value>
Polybos
+8  A: 

Template typedefs are not supported in the current C++ standard. There are workarounds, however:

template<typename T>
struct MyOwnMap {
  typedef std::map<std::string, T> Type;
};

MyOwnMap<int>::Type map;
Oskar N.
Excellent metaprograming-based answer. +1
paercebal
Except the resulting name will most likely not be much shorter...
UncleBens
@Uncle: He didn't say it was for length purposes, and his example takes the same number of keystrokes.
Dennis Zickefoose
+9  A: 

In C++98 and C++03 typedef may only be used on a complete type:

typedef std::map<int,int> IntToIntMap;

With C++0x there is a new shiny syntax to replace typedef:

using IntToIntMap = std::map<int,int>;

which also supports template aliasing:

template <
  typename Key,
  typename Value,
  typename Comparator = std::less<Key>,
  typename Allocator = std::allocator< std::pair<Key,Value> >
>
using myOwnMap = std::map<Key,Value,Comparator,Allocator>;

Here you go :)

Matthieu M.