Underscores, names and literal operators

廉价感情. 提交于 2019-12-23 20:15:37

问题


My question regarding underscores in names is partly answered here, but either the answer is incomplete or I do not fully understand it.

Sect. 2.14.8.7 of the C++11 standard declares this literal operator as an example:

long double operator "" _w(long double);

Besides declaring the operator, the standard and its example do two further things that, if viewed separately, each make sense:

  • it begins the name _w with an underscore; and
  • it puts the operator in the global namespace.

My question has two parts:

  1. According to the answer linked above, the name _w is not an identifier, or the identifier _w is not a name, or ... well, I'm confused.
  2. If _w is okay, then is the capitalized _W okay, too -- as in 60.0_W, meaning 60.0 watts? Or is the preprocessor likely to mishandle the capitalized version?

Undoubtedly like you, I am not in the habit of starting global names with underscores, a habit the standard's sect. 17.6.4.3.2.1 explicitly seems to deprecate. Therefore, if you can cast some additional light on the matter of underscores, names and literal operators, the light would be appreciated.


回答1:


Alright, I checked back with Richard Smith from the Clang team, and the _W part in your literal operator is indeed not a reserved identifier and/or name and it is also a seperate preprocessor token which will get expanded if it names a macro. This is in accordance with the standard subclauses 2.5, where an identifier is a preprocessor-token, and 2.2 which has macro expansion as part of phase 4, before preprocessor-tokens are replaced with just tokens of the language grammar, which happens in phase 7.

He also mentioned that, since the Portland meeting of the committee, you can say operator""_W, which will prevent macro expansion, since the _W is not a single identifier anymore. Clang trunk already implements this and compiles the following snippet:

#define _W _x

int operator""_W(unsigned long long){ return 42; }

int main(){
  int i = 1337_W;
}


来源:https://stackoverflow.com/questions/13793996/underscores-names-and-literal-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!