返回信息流“标识符不能包含两个连续的下划线,也不能以下划线开头后面紧跟一个大写字母,有些标识符(在函数外定义的标识符)不能以下划线开头”可为什么我在codeblocks里这样试了下不会有编译错误,这是为什么?
明白了,谢谢各位!
这是一条镜像帖。来源:北邮人论坛 / cpp / #48268同步于 2010/12/24
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
请教下c++ primer上关于变量命名规则的一段话
jkfbrant
2010/12/24镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
In addition to the keywords, the standard also reserves a set of identifiers for use in the library. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiersthose that are defined outside a functionmay not begin with an underscore.
我艹,找到了,恩,为了防止跟c++ keyword冲突
但为什么编译能通过呢,是编译器的问题吗
【 在 rebirthatsix 的大作中提到: 】
: In addition to the keywords, the standard also reserves a set of identifiers for use in the library. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiersthose that are defined outside a functionmay not begin with an underscore.
: 我艹,找到了,恩,为了防止跟c++ keyword冲突
: --
: ...................
我觉得不是,是对这段话的理解
它是告诉你,c++的标准库里将这些写法用于自己保留,也就是说没准哪天库里会新加一个类似
a__b的全局变量或者定义一个_Hlib这种宏,像这种下划线接大写字母的宏确实不少,那么如果你在你的程序里恰好写了一个变量叫这玩意,那就莫名完蛋了
只是意思使用这种约定很可能会和c++标准库冲突,并不是编译器强制
【 在 jkfbrant 的大作中提到: 】
: 但为什么编译能通过呢,是编译器的问题吗
: 【 在 rebirthatsix 的大作中提到: 】
: : In addition to the keywords, the standard also reserves a set of identifiers for use in the library. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiersthose that are defined outside a functionmay not begin with an underscore.
: ...................
这些都应该预留出来的...
标准库函数还有一些其他实现可能用到这样的名字,以防冲突...
【 在 jkfbrant (迦楼罗) 的大作中提到: 】
: “标识符不能包含两个连续的下划线,也不能以下划线开头后面紧跟一个大写字母,有些标识符(在函数外定义的标识符)不能以下划线开头”可为什么我在codeblocks里这样试了下不会有编译错误,这是为什么?
我理解也是这个意思,不是编译器强制,只是标准委员会与程序员间的一种约定。
其实,标准委员会他们也不好办。新的C++0x标准中,想要增加哈希表,但hashtable、hashset、hashmap等名字有很多第三方库都已经用了,没办法,只好换了个名字,都用unordered开头,unordered_set、unordered_map、unordered_multiset、unordered_mulitmap。
名字这东西,冲突了还真是不好办。