473,473 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Macro and template function

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
2 2798
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
4
by: jjleto | last post by:
I have a C program that uses in some parts macros with # and ## like in: #define GET_FUNC_DECL(name) char *get##name(); #define GET_FUNC_IMPL(name) char *get##name() { /* some stuff */; return...
4
by: Thomas Matthews | last post by:
Hi, I have several translation units (modules) which contain static {local} variables. These function have short global accessor functions. I would like to change these functions into macros,...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
9
by: jedi200581 | last post by:
Hello, I have a singleton class that I'm using through macros: //snippet #define DO_SOMETHING ( myArgument) \ { \ MyClass *myObject = MyClass::getInstance(); \...
12
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class...
2
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.