Strange behaviour with templates and #defines - c++

I have the following definitions:
template<typename T1, typename T2>
class Test2
{
public:
static int hello() { return 0; }
};
template<typename T>
class Test1
{
public:
static int hello() { return 0; }
};
#define VERIFY_R(call) { if (call == 0) printf("yea");}
With these, I try to compile the following:
VERIFY_R( Test1<int>::hello() );
this compiles fine
VERIFY_R( (Test2<int,int>::hello()) );
this also compiles fine, notice the parentheses around the call.
VERIFY_R( Test2<int,int>::hello() );
This, without the parentheses produces a warning and several syntax errors:
warning C4002: too many actual parameters for macro 'VERIFY_R'
error C2143: syntax error : missing ',' before ')'
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
fatal error C1004: unexpected end-of-file found
What's going on here?
This happens with VS2008 SP1.

The comma inside a macro can be ambiguous: an extra set of parentheses (your second example) is one way of disambiguating. Consider a macro
#define VERIFY(A, B) { if ( (A) && (B) ) printf("hi"); }
then you could write VERIFY( foo<bar, x> y ).
Another way of disambiguating is with
typedef Test1<int,int> TestII;
VERIFY_R( TestII::hello() );

The preprocessor is a dumb text replacement tool that knows nothing about C++. It interprets
VERIFY_R( Test1<int,int>::hello() );
as
VERIFY_R( (Test1<int), (int>::hello()) );
which calls VERIFY_R with too many parameters. As you noted, additional parentheses fix this:
VERIFY_R( (Test1<int,int>::hello()) );
The question remains, however, why you need the preprocessor anyway. The macro you used in your question could just as well be an inline function. If you real code doesn't do anything requiring the preprocessor, try to get rid of macros. They just cause pain.

The comma in <int, int> is treated as an argument separator for the macro, rather than for the template. The compiler therefore thinks you're calling VERIFY_R with two arguments (Test1<int and int>::hello()), when it requires only one. You need to use variadic macros to expand everything supplied to the macro:
#define VERIFY_R(...) { if ((__VA_ARGS__) == 0) printf("yea");}
It is generally a good idea to wrap macro arguments in parentheses, as well, to prevent other kinds of weird substitution errors.

The preprocessor doesn't know that < and > are supposed to be brackets, so it interprets the expression as two macro arguments, Test1<int and int>::hello(), separated by the ,. As you say, it can be fixed by surrounding the entire expression with parentheses, which the preprocessor does recognise as brackets.

I'm not sure if this is an error in your reporting here or the actual problem, but your last VERIFY_R is still referencing Test1, rather than Test2.

Related

using boost foreach with items that are themselves templates

I have a std::deque< std::pair<int, int> > that I would like to iterate over using BOOST_FOREACH.
I tried the following:
#define foreach_ BOOST_FOREACH
// declaration of the std::deque
std::deque< std::pair<int, int> > chosen;
foreach_( std::pair<int,int> p, chosen )
{
...
}
But when I compile this (in Visual Studio ) I get the following errors:
warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ')' before '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : ')'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ';' before '{'
1>c:\users\beeband\tests.cpp(133): error C2181: illegal else without matching if
What is the correct way to use BOOST_FOREACH with this deque?
The problem is the , as it is being used by the preprocessor to separate the macro arguments.
Possible solutions using typedef:
typedef std::pair<int, int> int_pair_t;
std::deque<int_pair_t> chosen;
foreach_( int_pair_t p, chosen )
// Or (as commented by Arne Mertz)
typedef std::deque<std::pair<int, int>> container_t;
container_t chosen;
foreach_(container_t::value_type p, chosen)
Possible replacements, both introduced in c++11, are:
range-for loop:
for (auto& : chosen)
{
}
lambdas:
std::for_each(std::begin(chosen),
std::end(chosen)
[](std::pair<int, int>& p)
{
});
As the author of BOOST_FOREACH, I ask you to please stop using it. It was a hack whose time has come and gone. Please, please, please use C++11's range-base for loops and let BOOST_FOREACH die.
Boosts foreach is a macro, which means it's handled by the preprocessor. And the preprocessor is pretty simple, and can't handle symbols with commas in them as it is used as a macro argument separator.

VS2008 C++ "interface" as a parameter name fails to compile

As the title says, I'm getting a compiler error in a VS2008 C++ program. I'm not sure how better to describe my problem than in code. The following compiles unless I uncomment the TEST line.
#include <windows.h>
#include <iostream>
using namespace std;
//#define TEST //<-- uncomment for error
#ifdef TEST
void test(void* interface)
{
return;
}
#endif
int main()
{
cout << "Hello World" << endl;
system("PAUSE");
return(0);
}
When uncommented I get the following errors:
1>main.cpp(7) : error C2332: 'struct' : missing tag name
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ')'
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ';'
1>main.cpp(7) : error C2059: syntax error : ')'
1>main.cpp(8) : warning C4094: untagged 'struct' declared no symbols
1>main.cpp(8) : error C2143: syntax error : missing ';' before '{'
1>main.cpp(8) : error C2447: '{' : missing function header (old-style formal list?)
This is unmanaged code, so I'm not sure what the issue with the word interface is. Is there any way to get this code to compile as is, or do I have to change every instance of the term interface to something else?
Thanks!
If your code needs to include Windows.h then you should avoid using the name interface as it's reserved for the use that the Windows SDK has reserved for it (essentially it's a synonym for the keyword struct). There are probably hacks to work around that problem (you could #undef interface after including the SDK headers), but you should probably avoid using that identifier.
The word interface is reserved by MSVC++, as it is a non-standard keyword added by Microsoft Compiler, which is used to define interface in MSVC++.
So use a different name for the parameter, something like this:
#ifdef TEST
void test(void* test_interface)
{
return;
}
#endif

C++ goto (rather than continue) syntactic oddity

I have the following code:
do
{
doStuffP1();
if (test)
{ goto skip_increment;
}
dostuffP2();
skip_increment:
// 1; // Only works if I remove the comment at line start.
} while (loop);
Which doesn't compile (VC++ 2010) with this error:
file_system_helpers.cpp(109) : error C2143: syntax error : missing ';' before '}'
If I change it to:
skip_increment:
1;
It compiles (and works).
Is this really a limitation of C++ syntax?
I assume the "1;" was supposed to be missing from your first code snippet?
Look at this grammar here: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
This defines labels only as a "labeled-statement". That is, a block body can contain label: <statement> anywhere in its sequence of contents, but the statement after the label is not optional. So this would make skip_increment: } invalid.
(And, OK, you're using C++ and not C; but I doubt if making allowances for extra uses of goto was something anyone cared much about while defining the C++ language.)

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

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!
The preprocessor has no understanding of C++! It simply performs lexical substitutions.
When you declare a multi-argument macro, the arguments are separated by a comma. Since you have a comma in your macro invocation, you're calling the macro with multiple parameters, despite it being declared to only take one argument.
Parentheses are understood by the PP as forming a token group, so everything inside a set of parentheses is one big token.
Macros are unaware of the language, and work only with lexical tokens. A comma separates arguemnts for a macro, thus the following code attemps to 'invoke' the macro with two arguments:
BOOL_CHECK_BOOL_RETURN(DoubleTemplated<int, bool>(thing));
DoubleTemplated<int and bool>(thing). That's the warning you are seeing, and cause of the other errors as well. The following is the correct way to protect against , in template arguments list:
BOOL_CHECK_BOOL_RETURN((DoubleTemplated<int, bool>(thing)));
In the line that doesn't compile, that comma is interpreted by the preprocessor as a delimiter of the macro arguments.
In the C99 standard (I haven't got the C++ standard to hand, but it will be very similar), we see the following in section 6.10.3:
The sequence of preprocessing tokens bounded by the outside-most
matching parentheses forms the list of arguments for the function-like
macro. The individual arguments within the list are separated by comma
preprocessing tokens, but comma preprocessing tokens between matching
inner parentheses do not separate arguments.
So that's why your second macro instantiation works.

Why does the order of my #includes matter? (C++)

I've created a header file called "list_dec.h", put it in a folder "C:\Headers", and set my compiler to include files from "C:\Headers", so now I can do things like
#include<list_dec.h>
int main(){return(0);}
but when I try to do something like
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
I get an error (not anything specific, just a huge list of syntax errors in "list_dec.h", which I know aren't real because I've been able to compile it as both a main.cpp file and a .h file in a separate project). However, when I change to order so "list_dec.h" is on top:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
all of the errors go away. So why does the order of the error matter?
NB: As far as I know, this occurs when I use "list_dec.h" with all header files, but the files I'm absolutely positive it occurs in are:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
EDIT: These are the errors I get when "list_dec.h" is below any other header:
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(51) : see reference to class template instantiation 'list<T,limit>' being compiled
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(14) : error C2059: syntax error : ')'
c:\headers\list_dec.h(14) : error C2238: unexpected token(s) preceding ';'
c:\headers\list_dec.h(69) : warning C4346: 'list<T,limit>::{ctor}' : dependent name is not a type
prefix with 'typename' to indicate a type
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(69) : error C2988: unrecognizable template declaration/definition
c:\headers\list_dec.h(69) : error C2059: syntax error : 'constant'
c:\headers\list_dec.h(69) : error C2059: syntax error : ')'
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(79) : error C2143: syntax error : missing ';' before '{'
c:\headers\list_dec.h(79) : error C2447: '{' : missing function header (old-style formal list?)
If it helps, these are the lines mentioned in the errors (14, 69, 78, and 79):
Line 14: list(const T& NULL); (A constructor for "list" class)
Line 69: inline list<T, limit>::list(const T& NULL): (Definition for the constructor, also, the colon at the end is intentional, It part of the definion ie: void x(int n): VAR(n).)
Line 78: inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)
Line 79: { (the begining of the list-copy contructor)
And a lot of people want to see the beginning of "list_dec.h":
template<class T, size_t limit>
class list
NB: These aren't the first lines, but they're where I think the problem is, the lines before them are simply an enumeration called "err".
EDIT: Just a note, "list_dec.h" contains no includes, defines, ifdefs, or anything precede with a '#'. Besides the enumeration, it only contains the "list" class declaration and the "list" class member function definitions.
Generally speaking it should not, however it may be possible for there to be conflicting definitions of symbols or preprocessor macros that end up confusing the compiler. Try to narrow down the size of the problem by removing pieces and includes from the conflicting header until you can see what is causing it.
In response to the error messages you posted, the symbol NULL is often implemented as a preprocessor macro for the number 0. This is so that you can easily use it as a null pointer. Therefore this:
list(const T& NULL);
Could be converted into this syntax error by the preprocessor:
list(const T& 0);
Change the name of the parameter to something other than NULL.
Note that here:
Line 14: list(const T& NULL); (A constructor for "list" class)
NULL is the name of standard macro - when a standard header file is included before list_dec.h it will most likely cause NULL to be defined which will in turn cause your code to look something like this to the compiler:
list(const T& 0);
The constant 0 above makes the line ill-formed C++. You might get more information by instructing your compiler to produce preprocessed output file.
Presumably list_dec.h is running into a macro that's defined in those other headers (or some headers they in turn include) -- hard to say which one without seeing the first error message and the relevant part of list_dec.h!
The actual errors would give a more specific clue, bt it means there's something in your include file that is screwing up the scan for the next one. The most common thing would be some kind of unclude #-directive, like a #if missing its #endif.
If the errors are random in nature, it could be a missing semi colon. The compiler will usually halt on that, but on occasion you get "lucky".
Otherwise, conflicting names or defines. Do you have anything named std for example?