By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Macro and template function

P: n/a
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;
}
----------------------------------

What is wrong?

Alex Vinokur
Aug 14 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alex Vinokur wrote:
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.
>

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
Aug 14 '08 #2

P: n/a
On Aug 13, 11:36 pm, red floyd <no.spam.h...@example.comwrote:
Alex Vinokur wrote:
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.
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.
Aug 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.