Templated function with two type parameters fails compile when used with an error-checking macro

Posted by SirPentor on Stack Overflow See other posts from Stack Overflow or by SirPentor
Published on 2011-11-12T01:38:04Z Indexed on 2011/11/12 1:51 UTC
Read the original article Hit count: 203

Filed under:
|
|

Because someone in our group hates exceptions (let's not discuss that here), we tend to use error-checking macros in our C++ projects. I have encountered an odd compilation failure when using a templated function with two type parameters. There are a few errors (below), but I think the root cause is a warning:

warning C4002: too many actual parameters for macro 'BOOL_CHECK_BOOL_RETURN'

Probably best explained in code:

#include "stdafx.h"


template<class A, class B>
bool DoubleTemplated(B & value)
{
    return true;
}

template<class A>
bool SingleTemplated(A & value)
{
    return true;
}

bool NotTemplated(bool & value)
{
    return true;
}

#define BOOL_CHECK_BOOL_RETURN(expr) \
    do \
    { \
        bool __b = (expr); \
        if (!__b) \
        { \
            return false; \
        } \
    } while (false) \

bool call()
{
    bool thing = true;

//  BOOL_CHECK_BOOL_RETURN(DoubleTemplated<int, bool>(thing));
//  Above line doesn't compile.

    BOOL_CHECK_BOOL_RETURN((DoubleTemplated<int, bool>(thing)));
//  Above line compiles just fine.

    bool temp = DoubleTemplated<int, bool>(thing);
//  Above line compiles just fine.


    BOOL_CHECK_BOOL_RETURN(SingleTemplated<bool>(thing));
    BOOL_CHECK_BOOL_RETURN(NotTemplated(thing));

    return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    call();
    return 0;
}

Here are the errors, when the offending line is not commented out:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>c:\junk\temp\test\test\test.cpp(38) : warning C4002: too many actual parameters for macro 'BOOL_CHECK_BOOL_RETURN'
1>c:\junk\temp\test\test\test.cpp(38) : error C2143: syntax error : missing ',' before ')'
1>c:\junk\temp\test\test\test.cpp(38) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(41) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(48) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(49) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(52) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(54) : error C2065: 'argv' : undeclared identifier
1>c:\junk\temp\test\test\test.cpp(54) : error C2059: syntax error : ']'
1>c:\junk\temp\test\test\test.cpp(55) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(58) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(60) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(60) : fatal error C1004: unexpected end-of-file found
1>Build log was saved at "file://c:\junk\temp\test\test\Debug\BuildLog.htm"
1>test - 12 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any ideas? Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates