Killrazor
Killrazor

Reputation: 7176

How to aliase a function with variable number of arguments?

I have a method in a class with this signature:

void addMessage_( std::string appender, LogLevel level /*= LOGLEVEL_INFO*/, char* msg, ... );

I want to create 'alias' of this method in this way:

void debugMsg( std::string appender, char* msg, ... ){
    addMessage(appender, LOGLEVEL_DEBUG, msg, ...);
}

My question is: do I need to expand the args to call the addMessage method? I don't want to replicate the code I each utility method, but I don't want to waste performance. What is the best solution?

Solved: I let my solution as a documentation:

void DEBUG_MSG(std::string appender, char* msg, ...){
    va_list argptr;
    va_start(argptr,msg);
    addMessage_(appender, LOGLEVEL_DEBUG, msg, argptr);
    va_end(argptr);
}

And the addMessage_ method:

    void CGlobalLog::addMessage_( std::string appender, LogLevel level, char* msg, va_list args ){

    int     len;
    char    *buffer;

    len = _vscprintf( msg, args ) // _vscprintf doesn't count
        + 1; // terminating '\0'

    buffer = (char*)malloc( len * sizeof(char) );

    vsprintf( buffer, msg, args ); // C4996
    // Note: vsprintf is deprecated; consider using vsprintf_s instead
    addMessage(buffer,appender,level);

    free( buffer );
}

Thanks!

Upvotes: 0

Views: 149

Answers (2)

orlp
orlp

Reputation: 117681

It's not possible that way (without macros), you must change your original function to take a va_list.

It is fully explained here: Passing variable number of arguments around

Upvotes: 2

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361402

Macro can solve that in just one line:

#define debugMsg(appender,msg,...)    addMessage(appender,LOGLEVEL_DEBUG, msg, __VA_ARGS__)

Or, you've to make use of va_list, va_start and va_end as explained here.

Upvotes: 0

Related Questions