简体   繁体   中英

C++ Replacing Functions with Macros

We have an existing function implementation in our C++ code:

void Function(int param)
{
    printf("In Function\n");
}

int main()
{
    Function(10);
    return 0;
}

I wish to change it to call another function (by help of a macro declaration) which would accept additional params like FILE and LINE (for debugging purpose) and then call the actual function:

#define Function(param) Function_debug(param, __FILE__,  __FUNCTION__,  __LINE__) \
{\
    printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
    Function(param);\
}

But the below code:

#include <stdio.h>

#define Function(param) Function_debug(param, __FILE__,  __FUNCTION__,  __LINE__) \
{\
    printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
    Function(param);\
}

void Function(int param)
{
    printf("In Function\n");
}

int main()
{
    Function(10);
    return 0;
}

Translates TO:

void Function_debug(int param, "temp.cpp", __FUNCTION__, 9) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(int param);}
{
    printf("In Function\n");
}

int main()
{
    Function_debug(10, "temp.cpp", __FUNCTION__, 16) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(10);};
    return 0;
}

which gives compilation errors.

Please direct me how to achieve the objective?

Normally you'd do something like this:

#if DEBUG
#define FUNCTION(param) Function_debug(param, __FILE__,  __FUNCTION__,  __LINE__)
#else
#define FUNCTION(param) Function(param)
#endif

void Function(int param)
{
    printf("In Function\n");
}

void Function_debug(int param, const char * file,  const char * func,  int line)
{
    printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
    Function(param);
}

int main()
{
    FUNCTION(10);
    return 0;
}

1st rule: use different name for your macro and the function that it replaces.
2nd rule: if using gcc, wrap the code block in parenthesis ({ }) to make your functions callable from as if printf("%f\\n",sqrt(2.0));

#define Function(a) ({ printf("Debug stuff\\n"); orig_function(a);})

You should first implement a second function and then in your macro just redirect your code to it, in your version you implement the function everywhere that use macro:

void function( int param )
{
    // do something interesting here
}
void function_debug( int param, char const* fileName, unsigned line, char const* fn)
{
    // debug position here
    function( param );
}

#ifndef NDEBUG
#    define Function( param )    function_debug( param, __FILE__, __LINE__, __FUNCION__ )
#else
#    define Function( param )    function( param )
#endif

can't you add the debug print into Function ?

void Function(int param)
{
    #ifdef DEBUG
    //debug stuff
    #endif
    printf("In Function\n");
}

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