这个:
enum Id {
START,
INDENT
};
struct State {
int line;
int column;
Id id;
};
...
lexer::State state = {0};
编译。
但如果我把Id id
作为结构的第一个元素,它就会停止。有人可以简单地向我解释为什么这两种结构被区别对待。
你得到一个错误,因为int
类型的常量零与id
类型的初始字段Id
不兼容,除非
1)你添加一个演员,像这样
lexer::State state = {static_cast<Id>(0)};
2)或使用START
代替零,像这样
lexer::State state = {START};
3)或零下降值初始化整个struct
(推荐):
lexer::State state = {};