Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to class in C++ macro

I want to write

struct Foo{
    MY_MACRO
};

and have that expand into

struct Foo{
    void bar(Foo&){}
};

How do I define MY_MACRO?

The only things I can come up with is the following:

#define MY_MARCO(X) void bar(X&){}
struct Foo{
    MY_MACRO(Foo)
};

This comes very close but is sub ideal, as I do not want to repeat the class name.

Unfortunately the following does not compile:

struct Foo{
    void bar(decltype(*this)&){}
};
like image 767
B.S. Avatar asked Dec 15 '25 13:12

B.S.


1 Answers

This is closely related this question. The answer is that you can not (yet) write something that uses the type of the class definition you are in. You will have to write a macro that includes the start of the class definition (i.e the struct Foo) and some mechanism to facilitate the typedef.

like image 191
Arne Mertz Avatar answered Dec 17 '25 04:12

Arne Mertz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!