Translations of this page?:

# と ##

#、 ## プリプロセッサ演算子は#defineプリプロセッサ命令と一緒に使用されます。

  • Using # causes the first argument after the # to be returned as a string in quotes.
  • Using ## concatenates what's before the ## with what's after it.
  • # を使用すると、#の後の最初の引数を、クオートされた文字列として返します。
  • ## を使用すると、##演算子の前後の内容を接合します。

#のサンプルコード:

     #define to_string( s ) # s

上記のようなマクロが定義されると、以下のようなコードが書かれた場合に、

     cout << to_string( Hello World! ) << endl;

以下のようなコードに置き換えられます。

     cout << "Hello World!" << endl;

##演算子のサンプルコード:

     #define concatenate( a, b ) a ## b
     ...
     int xy = 10;
     ...

上記のようなマクロが定義されたとすると、以下のようなコードがあると、

     cout << concatenate( x, y ) << endl;

以下のようなコードに置換されます。

     cout << xy << endl;

これを実行すると、もちろん、'10'という結果が標準出力に表示されます。

関連トピック: #define

 
• • • SitemapRecent changesRSScc