代码之家  ›  专栏  ›  技术社区  ›  boatcoder

何时“和”成为C++中的操作符

  •  25
  • boatcoder  · 技术社区  · 15 年前

    static const std::string and(" AND ");
    

    这会在g++中导致如下错误:

    Row.cpp:140: error: expected unqualified-id before '&&' token
    

    所以在诅咒了定义“和”为&的傻瓜之后&,我补充道

    #ifdef and
    #undef and
    #endif
    

    现在我明白了

    Row.cpp:9:8: error: "and" cannot be used as a macro name as it is an operator in C++
    

    这就引出了我的问题:“和”何时成为C++中的操作符?我找不到任何迹象表明它是,当然除了这个消息从g++

    7 回复  |  直到 15 年前
        1
  •  10
  •   user1641854    9 年前

    several such alternatives 在C++中定义。你可以使用开关来打开/关闭这些。

        2
  •  29
  •   interjay    15 年前

    来自C++03标准,第2.5节:

    2.5替代代币

    为某些运算符和标点符号提供了可选的标记表示。在语言的各个方面,除了拼写之外,每个替代标记的行为分别与其主要标记相同。表2中定义了一组替代标记。

    表2备用令牌

    alternative primary 
      <%         { 
      %>         } 
      <:         [ 
      :>         ] 
      %:         # 
      %:%:       ## 
      and        && 
      bitor      | 
      or         || 
      xor        ˆ 
      compl      ˜ 
      bitand     & 
      and_eq     &= 
      or_eq      |= 
      xor_eq     ˆ= 
      not        ! 
      not_eq     != 
    
        3
  •  14
  •   Jerry Coffin    15 年前

    and , or , xor , not bitand , bitor , compl , and_eq or_eq , xor_eq , , not_eq

        4
  •  9
  •   Micha Wiedenmann Lieven Keersmaekers    10 年前

    你可以用 -fno-operator-names 禁用此功能。或者,您可以命名您的 std::string 反对别的东西!

        5
  •  5
  •   Kirill V. Lyadvinsky    15 年前

    根据C++标准2.12,有预定义的预处理器令牌,它们用于 and 是其中之一。在新的C++标准中有新的2.12/2:

    保留,不得使用

    and and_eq bitand bitor compl not
    not_eq or or_eq xor xor_eq
    
        6
  •  4
  •   Peter Alexander    15 年前

        7
  •  3
  •   Troubadour    12 年前

    我不知道它是什么时候推出的,它很可能从一开始就存在,但我相信它存在的原因是作为一个替代品 && 对于具有受限字符集的人,即他们实际上没有“与”字符的人。

    还有很多其他的。 and_eq , or , compl not