[英]using vs. typedef for std::vector::iterator
I am having a problem when using the new C++11 using
keyword. 使用新的C ++ 11时
using
关键字时遇到问题。 As far as I understand, it's an alias for typedef
. 据我所知,它是
typedef
的别名。 But I cannot get it to compile. 但我无法编译。 I want to define an alias for an iterator of a
std::vector
. 我想为
std::vector
的迭代器定义一个别名。 If I use this everything works perfectly. 如果我使用它,一切都很完美。
typedef std::vector<fix_point>::iterator inputIterator;
But if I try: 但如果我尝试:
using std::vector<fix_point>::iterator = inputIterator;
The code doesn't compile with: 代码不能编译:
Error: 'std::vector<fix_point>' is not a namespace
using std::vector<fix_point>::iterator = inputIterator;
^
Why doesn't this compile? 为什么不编译?
You just have it backwards: 你只需要倒退:
using inputIterator = std::vector<fix_point>::iterator;
The alias syntax sort of mirrors the variable declaration syntax: the name you're introducing goes on the left side of the =
. 别名语法排序镜像变量声明语法:您引入的名称位于
=
的左侧。
typedef is a specifier that may be mixed with other specifiers. typedef是一个可以与其他说明符混合的说明符。 Thus the following typedef declarations are equivalent.
因此,以下typedef声明是等效的。
typedef std::vector<int>::iterator inputIterator;
std::vector<int>::iterator typedef inputIterator;
Opposite to the typedef declaration the alias declaration has strict order of specifiers. 与typedef声明相反,别名声明具有严格的说明符顺序。 According to the C++ Standard (7.1.3 The typedef specifier)
根据C ++标准(7.1.3 typedef说明符)
A typedef-name can also be introduced by an alias-declaration.
也可以通过别名声明引入typedef-name。 The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name.
using关键字后面的标识符变为typedef-name,并且该标识符后面的可选attribute-specifier-seq属于该typedef-name。 It has the same semantics as if it were introduced by the typedef specifier.
它具有与typedef说明符引入的语义相同的语义。 In particular, it does not define a new type and it shall not appear in the type-id.
特别是,它没有定义新类型,它不应出现在type-id中。
Thus you have to write 因此你必须写
using inputIterator = std::vector<int>::iterator ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.