对std :: vector :: iterator使用vs. typedef

Foa*_*aly 4 c++ using stdvector c++11

我在使用新的C++ 11 using关键字时遇到问题.据我所知,它是别名typedef.但我无法编译.我想为a的迭代器定义一个别名std::vector.如果我使用它,一切都很完美.

typedef std::vector<fix_point>::iterator inputIterator;
Run Code Online (Sandbox Code Playgroud)

但如果我尝试:

using std::vector<fix_point>::iterator = inputIterator;
Run Code Online (Sandbox Code Playgroud)

代码不能编译:

Error: 'std::vector<fix_point>' is not a namespace
using std::vector<fix_point>::iterator = inputIterator;
                            ^
Run Code Online (Sandbox Code Playgroud)

为什么不编译?

Bar*_*rry 14

你只需要倒退:

using inputIterator = std::vector<fix_point>::iterator;
Run Code Online (Sandbox Code Playgroud)

别名语法排序镜像变量声明语法:您引入的名称位于左侧=.


Vla*_*cow 9

typedef是一个可以与其他说明符混合的说明符.因此,以下typedef声明是等效的.

typedef std::vector<int>::iterator inputIterator;
std::vector<int>::iterator typedef inputIterator;
Run Code Online (Sandbox Code Playgroud)

与typedef声明相反,别名声明具有严格的说明符顺序.根据C++标准(7.1.3 typedef说明符)

也可以通过别名声明引入typedef-name.using关键字后面的标识符变为typedef-name,并且该标识符后面的可选attribute-specifier-seq属于该typedef-name.它具有与typedef说明符引入的语义相同的语义.特别是,它没有定义新类型,它不应出现在type-id中.

因此你必须写

using inputIterator = std::vector<int>::iterator ;
Run Code Online (Sandbox Code Playgroud)