关于c ++ 11:C ++:标识符规则不起作用

C++: identifier rules not working

本问题已经有最佳答案,请猛点这里访问。

根据Stanley Lipp的C++引物第五:

The standard also reserves a set of names for use in the standard library. The identifiers we define in our own programs may not contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an uppercase letter. In addition, identifiers defined outside a function may not begin with an underscore.

已编译以下所有内容:

g++ -std=c++11 -o test test.cc

1
2
3
4
5
int _I=40;
int main() {
    int __=10;
    int _B=20;
}

我认为它不应该编译..


文本中说"标准还保留了一组名称"——这并不一定意味着该表单的名称将导致编译器错误或警告。只是,如果您选择使用该表单的名称,它们可能与编译器或库定义的其他名称冲突。


引号意味着不能保证这样的名称有效:它们可能与实现使用的名称冲突。

当迈克·西摩在这里写评论时,

” You don't need to know the names in the standard library. You just need to know which names are reserved - which you described in one short sentence in the question. The compiler can't tell whether it's compiling your code or the library's, so it can't tell whether or not such names should be allowed - you just need to know this rule (which you do, since you asked a question about it) and follow it.