6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++ __VA_ARGS__ のメモ

Posted at

マクロで可変引数を扱うので使う

#define MYFUNC(s, ...) printf(s, __VA_ARGS__)

など.

問題: 空の __VA_ARGS__

MYFUNC("bora")

みたいに, __VA_ARGS__ が空になるのは本来は error になる.
(clang -Weverything にすると, -Wgnu-zero-variadic-macro-arguments warning が出る)

ダブルハッシュ(GNU 拡張)

##__VA_ARGS__ という表記で, 空の引数を扱える.

gcc 独自構文でした.

C++20

C++20 からは __VA_OPT__ が導入されひとまず解決

C++11 orlater

C++11 or later では, 可変引数テンプレートで対応できないか検討してみる.
たとえば, ロギングとかで __LINE__ などを使いたいときはどうしてもマクロ経由になるが, 実装をなるべくテンプレート関数で置き換える.

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?