MACRO( Foo<int, double> )

Discussion about everything. New games, 3d math, development tips...
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

MACRO( Foo<int, double> )

Post by MasterGod »

MACRO( Foo<int, double> )

the macro thinks it is being passed two arguments, namely Foo<int and double>, when in fact the construct is one C++ entity.
From: Sutter, Alexandrescu - C++ Coding Standards.

Can someone explain it to me? how can it be that one C++ entity is treated as two or what..?

Thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Not sure, but I suppose it's because macros are using other parsing rules. They are special insofar as they are evaluated before the normal compilation step, so they are not really like other c++ code. With macros it's like like you are copying the corresponding macrocode to the places where the macro is used before you start compiling. My guess would be that it just parses for something "left_parentheses", "parameters separated by comma", "right parentheses". So as soon as it sees a comma it thinks a new parameter starts.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
grafikrobot
Posts: 44
Joined: Wed Dec 26, 2007 11:42 pm

Post by grafikrobot »

Simply put... The preprocessor doesn't deal with the C++ grammar. It only deals with lexical tokens, and in particular there is a specific designator for preprocessor lexical tokens. In the example the preprocessor sees:

Code: Select all

"MACRO" "(" "Foo<int" "," "double>" ")"
That is, it doesn't try to parse the C++ semantics. If you really want to have such a macro work the usual work around is to use a double set of parens:

Code: Select all

MACRO((Foo<int,double>))
The Boost.PP library does this kind of thing in many places, and has some utilities for extracting the inside of that single expression argument.
Post Reply