nothrow
nothrow

Reputation: 16168

Typedef (alias) of an generic class

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?

Upvotes: 10

Views: 8748

Answers (5)

Muktadir Rahman
Muktadir Rahman

Reputation: 161

An alternative approach:

template <
        typename Key,
        typename Value,
        typename Comparator = std::less<Key>
>
class Map: public std::map<Key,Value, Comparator> {

};

Upvotes: -1

Arenoros
Arenoros

Reputation: 101

For C++ lower 11 only one way

template <...>
struct my_type : real_type<...> {}

Upvotes: 0

Oskar N.
Oskar N.

Reputation: 8587

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;

Upvotes: 14

Matthieu M.
Matthieu M.

Reputation: 299810

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

Upvotes: 27

Karl von Moor
Karl von Moor

Reputation: 8614

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>

Upvotes: 7

Related Questions