Search code examples
c++templatesc++11typename

Flexibility of template alias in C++0x


As I understand, template aliases in C++0x will allow us to do the following:

template <typename T>
using Dictionary = std::map< std::string, T >;

Dictionary<int> ints;
ints[ "one" ] = 1;
ints[ "two" ] = 2;

I have two questions:

First, will we be able to do this (bind to any type, or just templates):

template <typename Iter>
using ValueType = std::iterator_traits<Iter>::value_type;

Second, will using the aliases require usage of the typename keyword in templates, e.g.:

template <typename Iter>
typename ValueType<Iter> sum(Iter first, Iter last) { ... }
// ^ required?

Or is it required in the alias declaration?

using ValueType = typename std::iterator_traits<Iter>::value_type;
//                   ^ required?

Or neither?


Solution

  • The syntax is:

    template <typename Iter>
    using ValueType = typename std::iterator_traits<Iter>::value_type;
    

    as with your second one.

    Source: http://www2.research.att.com/~bs/C++0xFAQ.html#template-alias

    Their example is:

    template<int N>
        using int_exact = typename int_exact_traits<N>::type;  // define alias for convenient notation