Macro and template function

A

Alex Vinokur

Hi,

Here is my program

-------------- Program --------------
#define ADD(x,t) (x + t)

template <typename T, int N>
int foo()
{
return sizeof(T) * N;
}

int main()
{
int v1 = 5 + foo<int,10>();
int v2 = ADD(5, foo<int,10>());
return 0;
}
-----------------------------------------


Here is a compilation log
Compiler: aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
------------ Compilation log -----------------
"foo7.cpp", line 12: warning #2055-D: too many arguments in macro
invocation
int v2 = ADD(5, foo<int,10>());
^

"foo7.cpp", line 12: error #2439: expected a ">"
int v2 = ADD(5, foo<int,10>());
^

1 error detected in the compilation of "foo7.cpp".
---------------------------------------------------


Program after preprocessig

---------------------------------
template <typename T, int N>
int foo()
{
return sizeof(T) * N;
}

int main()
{
int v1 = 5 + foo<int,10>();
int v2 = (5 + foo<int); // Why?
return 0;
}
 
R

red floyd

Alex said:
Hi,

Here is my program

-------------- Program --------------
#define ADD(x,t) (x + t)

template <typename T, int N>
int foo()
{
return sizeof(T) * N;
}

int main()
{
int v1 = 5 + foo<int,10>();
int v2 = ADD(5, foo<int,10>());
return 0;
}
-----------------------------------------


Here is a compilation log
Compiler: aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
------------ Compilation log -----------------
"foo7.cpp", line 12: warning #2055-D: too many arguments in macro
invocation
int v2 = ADD(5, foo<int,10>());
^

"foo7.cpp", line 12: error #2439: expected a ">"
int v2 = ADD(5, foo<int,10>());
ADD(5, (foo<int, 10>()));

The preprocessor doesn't know about template definitions, so it sees the
comma in the template specialization as a macro parameter separator. It
does, however, know about parens, so putting the foo call in parens
helps it determine your intent.
 
D

diamondback

Alex said:
Here is my program
-------------- Program --------------
#define ADD(x,t) (x + t)
template <typename T, int N>
int foo()
{
return sizeof(T) * N;
}
int main()
{
int v1 = 5 + foo<int,10>();
int v2 = ADD(5, foo<int,10>());
return 0;
}
-----------------------------------------
Here is a compilation log
Compiler: aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
------------ Compilation log -----------------
"foo7.cpp", line 12: warning #2055-D: too many arguments in macro
invocation
int v2 = ADD(5, foo<int,10>());
^
"foo7.cpp", line 12: error #2439: expected a ">"
int v2 = ADD(5, foo<int,10>());

ADD(5, (foo<int, 10>()));

The preprocessor doesn't know about template definitions, so it sees the
comma in the template specialization as a macro parameter separator. It
does, however, know about parens, so putting the foo call in parens
helps it determine your intent.


1 error detected in the compilation of "foo7.cpp".
---------------------------------------------------
Program after preprocessig
---------------------------------
template <typename T, int N>
int foo()
{
return sizeof(T) * N;
}
int main()
{
int v1 = 5 + foo<int,10>();
int v2 = (5 + foo<int); // Why?
return 0;
}
----------------------------------
What is wrong?
Alex Vinokur

Thus, one reason macro functions are discouraged in C++. Why not use a
inline static function instead? Check out "Effective C++" (Meyers,
2005) for a good discussion on the topic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top