Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C++
#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++.
Posted
Updated 3-Dec-15 19:59pm
v3
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 2:16am    
This is C++. Don't use macro, use C++ instead. If you explain what you want to achieve, we will gladly try to help you with this. But the macro you are trying to use don't seem to make any practical sense.

Whatever it is, but there is no such concept as "overload macro"... "Operator overloading" has nothing to do with macro.

The question "Then how does it justify overloading concept of C++.?" is unclear. It's unclear what do is "it", what you would need to justify and why.

I suggest that you drop this question and try to continue to learn C++ concepts instead. You will quickly see that you don't really need macro, anyway, not in such cases.

—SA
Member 12018498 4-Dec-15 4:00am    
Thank you . I am using macro for logging the comments of my code. I wanted the same marco to be used for logging with overloading property. Like ENTER() and ENTER(thread id). with argument and without argument.So is it possible to do. I cannot avoid usage of macro. Because it was existing from long back in my project.
nv3 4-Dec-15 4:05am    
Operator overloading only applies to C++ functions and not to macros. Consider that macros are expanded by the preprocessor and and are hence are no longer visible to the following compilation steps. Although macros might look like functions to you they are in fact not.
Member 12018498 4-Dec-15 4:13am    
Thank you so much. I understood the difference now.
nv3 4-Dec-15 6:22am    
I have posted my comment as answered so that the question is closed.

Operator overloading only applies to C++ functions and not to macros. Consider that macros are expanded by the preprocessor and and are hence are no longer visible to the following compilation steps. Although macros might look like functions to you they are in fact not.
 
Share this answer
 
C++ provides many advanced features just to get rid of macros.
Well, that's not true, of course. However, unless you just want to stress the patience of the preprocessor, you'd better use C++ own features (e.g. templates) instead of such relics.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900