473,804 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
+ 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 2828
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...@e xample.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
34288
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
2
6998
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 delete operator with a macro. I can't replace the delete operator by using void* as the first parameter, because then my code will not be able to modify the calling function's pointer, nor would it get the source file name and line number.
4
1859
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 #name; } Is there a way to achieve this without macros in C++ ? With templates perhaps ?
4
4992
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, but still preserve the data hiding issue. How do I construct a macro to access static {local} variables in a module? The macro will be in a header file that is included in several modules. Example: timer_isr.c
19
7944
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
37
9012
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 inline function ? thanks for any help in advance ...
9
2039
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(); \ myObject->doSomething(myArgument); \ }; \
12
3234
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 Test1 {
2
2329
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. The reason why I need this: I want a fixed Index->Value mapping that should be part of a compile time API. The straight forward way to do this would be an array, e.g.: const int Mapper = {27, 225, 815, 4711, ..};
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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 we have to send another system
3
3005
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.