__va_args__

This is a discussion on __va_args__ within the C++ Programming forums, part of the General Programming Boards category; Code: void DebugPrint( const char* kpFormatString, ... ); #define PRINT(... ) { DebugPrint( __VA_ARGS__ ); } if( rc < 0 ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    109

    __va_args__

    Code:
    void DebugPrint( const char* kpFormatString, ... );
    #define PRINT(... ) { DebugPrint( __VA_ARGS__ ); }
    
    if( rc < 0 )
    {
    	PRINT( "blah blah 0x%x\n", rc );
    }
    else
    {
    	PRINT("glub glub\n");
    }
    Why do I need {} around my PRINT calls? If I don't place them there, I get:

    Code:
    error: expected primary-expression before 'else'
    I don't understand why it's trying to read in parameters past the delimited area.

    This is using VS2005.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by syzygy View Post
    Code:
    void DebugPrint( const char* kpFormatString, ... );
    #define PRINT(... ) { DebugPrint( __VA_ARGS__ ); }
    
    if( rc < 0 )
    {
    	PRINT( "blah blah 0x%x\n", rc );
    }
    else
    {
    	PRINT("glub glub\n");
    }
    Why do I need {} around my PRINT calls? If I don't place them there, I get:

    Code:
    error: expected primary-expression before 'else'
    I don't understand why it's trying to read in parameters past the delimited area.

    This is using VS2005.
    Because you are writing a semicolon at the end of the PRINT() macro invokation. Without braces, this semicolon serves as an empty statement, which terminates the if-statement, and causes the following else to become illegal. Change you definition of PRINT to this:

    Code:
    #define PRINT(... ) do { DebugPrint( __VA_ARGS__ ); } while(0)
    Now, the trailing semicolon will attach to the do-while, and everything should work.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    After macroexpansion, the code looks like this:

    Code:
    if (rc < 0)
    {
        { DebugPrint("blah blah 0x%x\n", rc); };
    }
    else
    {
       { DebugPrint("glub glub\n"); };
    }
    Now, if you remove the surrounding braces, it looks like this:

    Code:
    if (rc < 0)
        { DebugPrint("blah blah 0x%x\n", rc); };
    else
       { DebugPrint("glub glub\n"); };
    See the problem? The bolded semicolon effectively ends the "if" expression and you're left with a mismatched "else". Thus, you need the braces... or you could cut out the braces surrounding the `DebugPrint' call in the `PRINT' macro.
    Last edited by Ronix; 04-06-2010 at 11:22 AM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Thanks a lot guys. I see the problem now. I should have thought about it a bit more before asking.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21