C ++ 0x中模板别名的灵活性 [英] Flexibility of template alias in C++0x

查看:77
本文介绍了C ++ 0x中模板别名的灵活性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  template< T> 
使用Dictionary = std :: map< std :: string,T> ;;

字典< int> ints;
ints [one] = 1;
ints [two] = 2;

我有两个问题:



,我们将能够这样做(绑定到任何类型,或只是模板):

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

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

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

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

 使用ValueType = typename std :: iterator_traits< Iter> :: value_type; 
// ^必需?

或两者皆不?

解决方案

语法是:

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


$ b


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



他们的例子是:

  template< int N> 
using int_exact = typename int_exact_traits< N> :: type; //为方便符号定义别名


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

这篇关于C ++ 0x中模板别名的灵活性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆