使用新的C ++ 11 using
关键字时遇到问题。据我了解,它是typedef
的别名。但我无法编译。我想为std::vector
的迭代器定义别名。如果我使用它,一切都很完美。
typedef std::vector<fix_point>::iterator inputIterator;
但如果我尝试:
using std::vector<fix_point>::iterator = inputIterator;
代码无法编译:
Error: 'std::vector<fix_point>' is not a namespace
using std::vector<fix_point>::iterator = inputIterator;
^
为什么不编译?
答案 0 :(得分:14)
你只是倒退了:
using inputIterator = std::vector<fix_point>::iterator;
别名语法排序镜像变量声明语法:您引入的名称位于=
的左侧。
答案 1 :(得分:9)
typedef是一个可以与其他说明符混合的说明符。因此,以下typedef声明是等效的。
typedef std::vector<int>::iterator inputIterator;
std::vector<int>::iterator typedef inputIterator;
与typedef声明相反,别名声明具有严格的说明符顺序。根据C ++标准(7.1.3 typedef说明符)
也可以通过别名声明引入typedef-name。该 using关键字后面的标识符变为typedef-name和 标识符appertains后面的可选attribute-specifier-seq 到那个typedef-name。它具有与它相同的语义 由typedef说明符引入。特别是,它没有定义 一个新类型,它不应出现在type-id。
中
因此你必须写
using inputIterator = std::vector<int>::iterator ;