I want to overload a macro based on the number of arguments
For eg: BAR()--> should be called when I call it without an argument
BAR(x)--> when called with argument.
I saw many similar questions posted in other sites and I could see that the solution suggested for overloading is by changing the name of the macro like BAR(), BAR_Z(x).
One of the solution posted was:
#define MY_GLUE(x, y) x y
#define MY_ASSERT1(x) \
do \
{\
do something with x \
} while (false);
#define MY_ASSERT2(x, y) \
do \
{\
do other things with x and y \
} while (false);
#define MY_ASSERT_CHOOSE_HELPER2(count) MY_ASSERT##count
#define MY_ASSERT_CHOOSE_HELPER1(count) MY_ASSERT_CHOOSE_HELPER2(count)
#define MY_ASSERT_CHOOSE_HELPER(count) MY_ASSERT_CHOOSE_HELPER1(count)
#define ASSERT(...) \
MY_GLUE(MY_ASSERT_CHOOSE_HELPER(COUNT_ARGS(__VA_ARGS__)), (__VA_ARGS__))
void test(void) {
int a = 0, b = 1;
ASSERT(a == 0);
ASSERT(a == 0, b == 1);
}
Here MY_ASSERT1 and MY_ASSERT_2 are macros.
As per my knowledge on operator overloading the name of the function remains the same, and the specific function is called based on the type and number of arguments.
Where as I see that the solution suggested had different name.
Then how does it justify overloading concept of C++.?
Please correct me if I am mistaken, as I am new to C++.