3
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.

[備忘録]マクロの引数の数でオーバーロード

Posted at

はじめに

備忘録です。

シンプル版


// 必要次第追加
#define TEST_OVERLOAD(e1,e2,e3,NAME,...) NAME

#define TEST_1(x) x
#define TEST_2(x, y) x + y
#define TEST_3(x, y, z) x * y * z
#define TEST(...) TEST_OVERLOAD(__VA_ARGS__,TEST_3, TEST_2, TEST_1)(__VA_ARGS__)

少し一般化した版

アンスコ引数数で決め打ちのパターン

// 必要次第追加
#define GET_VA_SIZE( e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, COUNT, ... ) COUNT
#define VA_SIZE( ... ) GET_VA_SIZE( __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 )

#define PP_CAT( a, b ) a ## b
#define PP_OVERLOAD(macro, count) PP_CAT( macro ## _, count )

#define TEST_1(x) x
#define TEST_2(x, y) x + y
#define TEST_3(x, y, z) x * y * z
#define TEST(...) PP_OVERLOAD(TEST, VA_SIZE(__VA_ARGS__))(__VA_ARGS__)

参考
https://www.it-swarm-ja.tech/ja/c++/マクロは引数の数によってオーバーロードできますか?/1073435042/

3
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
3
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?