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?
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
typename
is required when a member type follows the ::
operator and a template-id precedes it.
The usage of typename
you mention isn't specific to template aliases nor is it required unless you're aliasing to a member such as ::type
, but that is a common use case.
For example, there's no typename
when introducing a simple alias name to an existing template.
template< typename x >
class bar;
template< typename x >
using foo = bar< x >; // no typename needed
精彩评论