Namespaces
Variants

Alternative operator representations

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

C++(및 C) 소스 코드는 ISO 646:1983 불변 문자 집합을 포함하는 모든 비-ASCII 7비트 문자 집합으로 작성될 수 있습니다. 그러나 여러 C++ 연산자와 구두점은 ISO 646 코드셋 외부의 문자를 필요로 합니다: {, }, [, ], #, \, ^, |, ~ . 이러한 기호 중 일부 또는 전체가 존재하지 않는 문자 인코딩(예: 독일의 DIN 66003 )에서도 사용할 수 있도록, C++는 ISO 646 호환 문자로 구성된 다음과 같은 대체 표현을 정의합니다.

목차

대체 토큰

여러 연산자 및 ISO646 문자가 아닌 문자를 사용하는 다른 토큰들에 대해 대체 스펠링이 존재합니다. 언어의 모든 측면에서, 각 대체 토큰은 스펠링을 제외하고 기본 토큰과 완전히 동일하게 동작합니다( 문자열화 연산자 는 스펠링을 가시적으로 만들 수 있습니다). 두 글자로 이루어진 대체 토큰들은 때때로 "이중 문자(digraphs)"라고 불립니다. 4글자 길이임에도 불구하고, % : % : 또한 이중 문자로 간주됩니다.

기본형 대체형
&& and
&= and_eq
& bitand
| bitor
~ compl
! not
!= not_eq
|| or
|= or_eq
^ xor
^= xor_eq
{ <%
} %>
[ <:
] :>
# %:
## %:%:

트라이그래프 (C++17에서 제거됨)

다음 세 문자 그룹(trigraph)들은 주석과 문자열 리터럴이 인식되기 전에 파싱됩니다 , 그리고 각 trigraph의 등장은 해당하는 기본 문자로 대체됩니다:

기본 문자 삼중 문자
{ ??<
} ??>
[ ??(
] ??)
# ??=
\ ??/
^ ??'
| ??!
~ ??-

삼중문자가 초기에 처리되기 때문에, // Will the next line be executed?????/ 와 같은 주석은 실제로 다음 줄을 주석 처리하게 되며, "Enter date ??/??/??" 와 같은 문자열 리터럴은 "Enter date \\ ??" 로 파싱됩니다.

참고 사항

문자 & ! 는 ISO-646에서 불변이지만, 더 제한적인 역사적 문자 집합을 수용하기 위해 이러한 문자를 사용하는 토큰에 대한 대체 표현이 여전히 제공됩니다.

동등 연산자 == 에 대한 대체 철자(예: eq )는 존재하지 않습니다. 왜냐하면 문자 = 가 지원되는 모든 문자 집합에 존재했기 때문입니다.

C와의 호환성

동일한 단어들은 C 프로그래밍 언어에서 include 파일 <iso646.h> 에 매크로로 정의되어 있습니다. C++에서는 이것들이 언어에 내장되어 있기 때문에, C++ 버전의 <iso646.h> <ciso646> 는 아무것도 정의하지 않습니다. 그러나 논-워드 이중문자(예: < % )는 코어 언어의 일부이며, 어떤 헤더도 포함하지 않고 사용할 수 있습니다(그렇지 않으면, # 가 없는 문자 집합에서는 사용할 수 없을 것입니다).

키워드

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

예제

다음 예제는 여러 대체 토큰의 사용법을 보여줍니다.

%:include <iostream>
struct X
<%
    compl X() <%%> // destructor
    X() <%%>
    X(const X bitand) = delete; // copy constructor
    // X(X and) = delete; // move constructor
    bool operator not_eq(const X bitand other)
    <%
       return this not_eq bitand other;
    %>
%>;
int main(int argc, char* argv<::>) 
<%
    // lambda with reference-capture:
    auto greet = <:bitand:>(const char* name)
    <%
        std::cout << "Hello " << name
                  << " from " << argv<:0:> << '\n';
    %>;
    if (argc > 1 and argv<:1:> not_eq nullptr)
        greet(argv<:1:>);
    else
        greet("Anon");
%>

가능한 출력:

Hello Anon from ./a.out

참고문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 5.5 대체 토큰 [lex.digraph]
  • C++20 표준(ISO/IEC 14882:2020):
  • 5.5 대체 토큰 [lex.digraph]
  • C++17 표준(ISO/IEC 14882:2017):
  • 5.5 대체 토큰 [lex.digraph]
  • C++14 표준(ISO/IEC 14882:2014):
  • 2.4 Trigraph sequences [lex.trigraph]
  • 2.6 Alternative tokens [lex.digraph]
  • C++11 표준(ISO/IEC 14882:2011):
  • 2.4 삼중자 시퀀스 [lex.trigraph]
  • 2.6 대체 토큰 [lex.digraph]
  • C++03 표준(ISO/IEC 14882:2003):
  • 2.3 트라이그래프 시퀀스 [lex.trigraph]
  • 2.5 대체 토큰 [lex.digraph]
  • C++98 표준(ISO/IEC 14882:1998):
  • 2.3 트라이그래프 시퀀스 [lex.trigraph]
  • 2.5 대체 토큰 [lex.digraph]

참고 항목

C 문서 에 대한 대체 연산자 및 토큰