C++ 0x中模板别名的灵活性

Pet*_*der 18 c++ templates typename c++11

据我所知,C++ 0x中的模板别名将允许我们执行以下操作:

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

Dictionary<int> ints;
ints[ "one" ] = 1;
ints[ "two" ] = 2;
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

首先,我们能够做到这一点(绑定到任何类型,或只是模板):

template <typename Iter>
using ValueType = std::iterator_traits<Iter>::value_type;
Run Code Online (Sandbox Code Playgroud)

其次,使用别名需要typename在模板中使用关键字,例如:

template <typename Iter>
typename ValueType<Iter> sum(Iter first, Iter last) { ... }
// ^ required?
Run Code Online (Sandbox Code Playgroud)

或者在别名声明中是否需要它?

using ValueType = typename std::iterator_traits<Iter>::value_type;
//                   ^ required?
Run Code Online (Sandbox Code Playgroud)

或者都不是?

Cas*_*Cow 16

语法是:

template <typename Iter>
using ValueType = typename std::iterator_traits<Iter>::value_type;
Run Code Online (Sandbox Code Playgroud)

和你的第二个一样.

资料来源:http: //www2.research.att.com/~bs/C++0xFAQ.html#template-alias

他们的例子是:

template<int N>
    using int_exact = typename int_exact_traits<N>::type;  // define alias for convenient notation
Run Code Online (Sandbox Code Playgroud)