Panwen Wang
Panwen Wang

Reputation: 3825

c++ get macro name in macro

How to get the macro name inside a macro? Say we have:

#include <iostream>
using std::cout;
using std::endl;

#define MACRO() \
cout << __MACRO_NAME__ << endl

int main () {
    MACRO();
    return 0;
}

Expected output:

MACRO

Upvotes: 1

Views: 1425

Answers (1)

Milan Patel
Milan Patel

Reputation: 422

Did little bit of research and I don't think that is doable in c++.

But you could use this:

#define MACRO2(x) cout << #x << endl
#define MACRO MACRO2(MACRO)

In this you can use MACRO2 to do the task of MACRO and you can also access name of MACRO as an argument x.

Upvotes: 2

Related Questions