c++overloadingc-preprocessorvariadic-macros

Overload macro as variable and function


First, there are a lot of posts on overloading macros:

However, all of them ask the question about variadic macros.

What I'd like to ask is if the following is possible:

#define FOO 3
#define FOO(a,b) a+b

int main() {
  int a = FOO(0,1);
  int b = FOO;
  std::cout << a + b;
  return 0;
}

I'd like for it to work on clang as well.


Solution

  • It's not a macro, but looks like it

    struct SmartMacro {
        constexpr operator int() const noexcept { return 3; }
        constexpr int operator()(int a, int b) const noexcept { return a + b; }
    };
    constexpr SmartMacro FOO;
    
    int main() {
        int a = FOO(0,1);
        int b = FOO;
        std::cout << a + b;
        return 0;
    }