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文字を使用する代替スペルが存在します。言語のすべての側面において、各代替トークンはそのスペルを除き( 文字列化演算子 によりスペルが可視化される場合があります)、プライマリトークンと完全に同じように動作します。2文字の代替トークンは「ダイグラフ」と呼ばれることがあります。4文字の長さであるにもかかわらず、 % : % : もダイグラフと見なされます。

プライマリ 代替表記
&& and
&= and_eq
& bitand
| bitor
~ compl
! not
!= not_eq
|| or
|= or_eq
^ xor
^= xor_eq
{ <%
} %>
[ <:
] :>
# %:
## %:%:

トライグラフ (C++17で削除)

以下の3文字グループ(トライグラフ)は、 コメントと文字列リテラルが認識される前に解析され 、各トライグラフの出現は対応する基本文字で置き換えられます:

基本文字 トライグラフ
{ ??<
} ??>
[ ??(
] ??)
# ??=
\ ??/
^ ??'
| ??!
~ ??-

トライグラフは早期に処理されるため、 // Will the next line be executed?????/ のようなコメントは実質的に次の行をコメントアウトし、 "Enter date ??/??/??" のような文字列リテラルは "Enter date \\ ??" として解析されます。

注記

文字 & および ! はISO-646の下で不変ですが、さらに制限の厳しい歴史的な文字セットに対応するため、これらの文字を使用するトークンに対して代替表現が提供されています。

等価演算子 == に対する代替スペル(例: eq など)は存在しません。これは、文字 = が全てのサポートされている文字セットに存在していたためです。

Cとの互換性

同じ単語はCプログラミング言語ではインクルードファイル <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() <%%> // デストラクタ
    X() <%%>
    X(const X bitand) = delete; // コピーコンストラクタ
    // X(X and) = delete; // ムーブコンストラクタ
    bool operator not_eq(const X bitand other)
    <%
       return this not_eq bitand other;
    %>
%>;
int main(int argc, char* argv<::>) 
<%
    // 参照キャプチャを使用したラムダ:
    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 トライグラフシーケンス [lex.trigraph]
  • 2.6 代替トークン [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 documentation for Alternative operators and tokens
日本語訳:
Cドキュメント 代替演算子とトークン
HTMLタグ、属性、およびC++固有の用語は翻訳せず、元のフォーマットを保持しました。「C documentation」は「Cドキュメント」に、「for」は「の」に、「Alternative operators and tokens」は「代替演算子とトークン」に翻訳しました。