简体   繁体   中英

Call a member function with the same name as macro

Consider the following:

struct S {
    void f() {}
};

#define f 42

int main() {
    S s;
    s.f(); // error: expected unqualified-id
}

How to call a member function S::f without undefining the macro f or changing member function name? Is it even possible?

If the macro was defined as #define f() 42 (with parentheses), I could fix the issue like (s,f)() . But there are no parentheses in macro definition.

How to call a member function S::f without undefining the macro f or changing member function name? Is it even possible?

It isn't possible.

Problems such as this are the reason why everyone recommends avoiding macros.

but there is WinAPI macros like SendMessage expanded to SendMessageA or SendMessageW depending on Unicode settings.

Those are function-style macros which as you know can be worked around using parenthesis. Dancing around the macros defined in various system headers is the harsh reality when you include system headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM