Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef (alias) of an generic class

Tags:

c++

templates

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?

like image 597
nothrow Avatar asked Aug 28 '10 14:08

nothrow


3 Answers

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

like image 73
Matthieu M. Avatar answered Sep 24 '22 05:09

Matthieu M.


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

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

MyOwnMap<int>::Type map;
like image 25
Oskar N. Avatar answered Sep 21 '22 05:09

Oskar N.


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>
like image 36
Karl von Moor Avatar answered Sep 23 '22 05:09

Karl von Moor