下划线,名称和文字操作符 [英] Underscores, names and literal operators

查看:186
本文介绍了下划线,名称和文字操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对名称中下划线的问题部分地回答了这里,但是答案不完整,或者我不完全理解



Sect。 C ++ 11标准的2.14.8.7声明此运算符作为示例:

  long double operator_w(long double); 

除了声明操作符之外,标准及其示例还做了两个事情,如果单独查看,有意义:




  • 它以下划线开始命名 _w



我的问题有两个部分:


  1. 根据上述回答, / a>名称 _w 不是标识符或标识符 _w 如果 _w 没问题,那么就是 60.0_W ,即 60.0 watts?的资本化 _W em>

  2. 无疑地像你一样,我不习惯启动全局名称与下划线,习惯标准的节。 17.6.4.3.2.1显然似乎不赞成。因此,如果你可以对下划线,名字和字面操作符进行一些额外的光线,那么光将会被欣赏。

    解决方案

    好的,我从Clang团队回来跟Richard Smith,并且你的文本运算符中的 _W 部分不是一个保留的标识符和/或名字,它也是一个单独的预处理器令牌,如果它命名一个宏,它将被扩展。这符合标准子条款2.5,其中标识符预处理器标记,2.2在预处理器标记之前具有作为阶段4一部分的宏扩展替换为语言语法的令牌,这发生在阶段7中。



    他还提到,因为委员会的波特兰会议,你可以说 operator_ W 这将阻止宏扩展,因为 _W 不再是一个标识符。 Clang trunk已经实现了这一点,并编译了以下代码片段:

      #define _W _x 

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

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


    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.

    解决方案

    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;
    }
    

    这篇关于下划线,名称和文字操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆