1

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.

αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
  • This is why C macros should always be in `UPPER_SNAKE_CASE`. – Dai Dec 01 '21 at 20:53
  • 1
    Does this answer your question? [Macro and function with same name](https://stackoverflow.com/questions/1951885/macro-and-function-with-same-name) – Dai Dec 01 '21 at 20:53
  • 2
    Change `#define f 42` to `constexpr auto f = 42;` – NathanOliver Dec 01 '21 at 20:55
  • 4
    Just one of the reasons [macros are evil](https://stackoverflow.com/q/14041453/10077). – Fred Larson Dec 01 '21 at 20:55
  • 1
    Once upon a time when I was younger, I named a variable `strlen`. Bad move. The `strlen` "function" had been implemented as a macro and rather than getting a, "Hey, bonehead, `strlen`s already in use" error, I got a dozen pages of completely nonsensical error diagnostics. – user4581301 Dec 01 '21 at 20:59
  • @Dai I don't see an answer in question linked by you since my macro doesn't have parentheses as i noted in my question. I agree with you about `UPPER_CASE`, but there is WinAPI macros like `SendMessage` expanded to `SendMessageA` or `SendMessageW` depending on Unicode settings. – αλεχολυτ Dec 01 '21 at 21:03
  • 1
    Are you allowed to change the definition of `S` at all? Can you add another member function to `S` that just calls `f`? – Brian Bi Dec 01 '21 at 21:04
  • @BrianBi that make sense, could be a solution in my case, but I'm still hoping about changing only `main()` part. – αλεχολυτ Dec 01 '21 at 21:07
  • Why not drop the macro and make `f` into what @NathanOliver suggested above? – Ted Lyngmo Dec 01 '21 at 21:08
  • @TedLyngmo I can't change macro to const since it's not my code (in particular WinAPI part). – αλεχολυτ Dec 01 '21 at 21:14
  • @αλεχολυτ Why can't you use `#undef` _inside_ a function, like in @Felipe's answer? – Dai Dec 01 '21 at 21:15
  • @αλεχολυτ Is your **actual** problem with a macro in a Win32 header? If so, then you should edit your question to show the *actual* macro you want to work-around instead of using a contrived example. – Dai Dec 01 '21 at 21:16
  • 1
    Related / possible dupe: https://stackoverflow.com/q/1543736 (The answer being to use [`push_macro`](https://gcc.gnu.org/onlinedocs/gcc/Push_002fPop-Macro-Pragmas.html)) – Artyer Dec 01 '21 at 21:25
  • @Dai question crux doesn't changed if I change `f` to `SendMessage` for instance. I used `f` just for brevity. In real code I can even undef the macro, but I'm looking for approach without it (or the answer that is not possible without `#undef`). – αλεχολυτ Dec 01 '21 at 21:26
  • @Dai see my comment below deleted answer (I know you can see it). `#undef` is too obvious way. – αλεχολυτ Dec 01 '21 at 21:33
  • @αλεχολυτ So what's wrong with a "too obvious" solution? – Dai Dec 01 '21 at 21:34
  • @Dai obvious solutions prevent deeper learning the language. – αλεχολυτ Dec 01 '21 at 21:41

1 Answers1

3

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 in standard C++.

Problems such as this are the reasons why everyone recommends avoiding macros, and to separate the naming conventions between macro and non-macro identifiers.

In non standard C++ however, it is possible. Example using push and pop macro language extension:

#pragma push_macro("f")
#undef f
s.f();
#pragma pop_macro("f")

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

Dancing around the macros defined in various system headers is the harsh reality when you include system headers. When encountering these, it's often best to refrain from using the conflicting identifiers.

αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
eerorika
  • 232,697
  • 12
  • 197
  • 326