[PR]

# 前処理演算子を使う

戻る

 文字列化演算子 # は展開したマクロ引数を文字列定数に変換します。

使用例

	// 属性値と属性名の関係一覧
	static const struct
		{
			      int   flag; // 属性フラグ
			const char *name; // 属性フラグの名前
		}
	g_attributes[] =
		{
	#define _( flag ) { flag, #flag }
			_( attr_readonly ),
			_( attr_hidden ),
			_( attr_system ),
			_( attr_directory ),
			_( attr_archive ),
			_( attr_device ),
			_( attr_normal ),
			_( attr_temporary ),
			_( attr_sparse_file ),
			_( attr_reparse_point ),
			_( attr_compressed ),
			_( attr_offline ),
			_( attr_not_content_indexed ),
			_( attr_encrypted ),
	#undef _ // #define _( flag )
		};

	// 属性値 flags に指定されている属性を列挙する。
	{for( int i = 0; i < numof( g_attributes ); ++i )
	{
		if ( 0 != (flags & g_attributes[ i ].flag) )
		{
			puts( g_attributes[ i ].name );
		}
	}}

 上記の例で、前処理指令 #define から前処理指令 #undef までのコードは以下のように展開されます。

	{ attr_readonly, "attr_readonly" },
	{ attr_hidden, "attr_hidden" },
	{ attr_system, "attr_system" },
	{ attr_directory, "attr_directory" },
	{ attr_archive, "attr_archive" },
	{ attr_device, "attr_device" },
	{ attr_normal, "attr_normal" },
	{ attr_temporary, "attr_temporary" },
	{ attr_sparse_file, "attr_sparse_file" },
	{ attr_reparse_point, "attr_reparse_point" },
	{ attr_compressed, "attr_compressed" },
	{ attr_offline, "attr_offline" },
	{ attr_not_content_indexed, "attr_not_content_indexed" },
	{ attr_encrypted, "attr_encrypted" },

Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
『新妻LOVELY×CATION』を応援しています!