C++
1template<typename T>
2T MAX(T x, T y) { return x > y ? x : y; }
コレ↑をマクロで(むりくり)実現するココロミ↓
C
1#define name2(x,y) x##y
2#define declare(x,y) name2(x,declare)(y)
3#define implement(x,y) name2(x,implement)(y)
4
5#define MAXdeclare(type) \
6type MAX(type)(type x, type y);
7
8#define MAXimplement(type) \
9type MAX(type)(type x, type y) { \
10 return x > y ? x : y; \
11}
12
13#define MAX(type) name2(MAX,type) // MAX(ほげほげ) -> MAXほげほげ
14
15#include <stdio.h>
16
17declare(MAX,int) // int MAXint(int,int) を宣言
18declare(MAX,float) // float MAXfloat(float,float) を宣言
19
20implement(MAX,int) // int MAXint(int,int) を定義
21implement(MAX,float) // float MAXfloat(float,float) を定義
22
23int main() {
24 // MAXint(int,int) を呼ぶ
25 printf("%d, %d -> %d\n", 2, 3, MAX(int)(2,3));
26 // MAXfloat(float,float) を呼ぶ
27 printf("%f, %f -> %f\n", 2.0f, 3.0f, MAX(float)(2.0f,3.0f));
28 return 0;
29}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/29 04:10