473,624 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with defining a macro?

I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?
Nov 7 '06 #1
4 1850
In article <6G64h.9243$Wd5 .1258@trnddc05> ,
Max TenEyck Woodbury <ma*@mtew.isa-geek.netwrote:
>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.
>Is it possible to write such a macro?
>If so, could you please provide an example?
Not possible in C89: in C89, macros only have no arguments
("object like macro") or a fixed-length list of arguments
("function like macro").

Not possible in C99: in C99, you cannot declare a function-like
macro that might be handed no arguments at all. function-like
macros in C99 must be declared with at least one fixed argument.

I have not looked at C99 handling of macros with variable argument
lists, so I do not know if you can somehow iterate over the arguments
or otherwise indicate that you want something to be done to each of
them in turn.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Nov 7 '06 #2

Max TenEyck Woodbury wrote:
I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?
It is not possible to write such a macro in C89 or C99, AFAIK.

This sounds like a potential case of preprocessor abuse; what is the
purpose of all these macros? Could the same purpose be served using a
set of functions (which can be written to take variable-sized argument
lists)?

Nov 7 '06 #3
Walter Roberson wrote:
In article <6G64h.9243$Wd5 .1258@trnddc05> ,
Max TenEyck Woodbury <ma*@mtew.isa-geek.netwrote:
>>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

>>Is it possible to write such a macro?

>>If so, could you please provide an example?


Not possible in C89: in C89, macros only have no arguments
("object like macro") or a fixed-length list of arguments
("function like macro").

Not possible in C99: in C99, you cannot declare a function-like
macro that might be handed no arguments at all. function-like
macros in C99 must be declared with at least one fixed argument.
I have reason to believe that your last sentence is incorrect.
While that is true of functions, it is not true of macros. In
particular the following works:

#define STR(...) # __VA_ARGS__

which allows the argument list to contain commas.
>
I have not looked at C99 handling of macros with variable argument
lists, so I do not know if you can somehow iterate over the arguments
or otherwise indicate that you want something to be done to each of
them in turn.
Well that is the question I asked...
Nov 8 '06 #4
John Bode wrote:
Max TenEyck Woodbury wrote:
>>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?


It is not possible to write such a macro in C89 or C99, AFAIK.
If it is possible, and I am not sure it is, it almost certainly
has to use the ## operator to get around expansion suppression
on the rescan.
>
This sounds like a potential case of preprocessor abuse; what is the
purpose of all these macros? Could the same purpose be served using a
set of functions (which can be written to take variable-sized argument
lists)?
This is needed to build a table with fairly complex entries at compile
time. I have some (slightly ugly) macros that build elements of an
array initializer, but I would like to make it a bit simpler to the
users by hiding more of the implementation details.

So no, it can not be done with functions.
Nov 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2735
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble offering. =================================================== #include <stdio.h> #include <stdlib.h>
8
4339
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I have done this as follows: LPOLESTR dunk_data = (LPOLESTR)T2CW(data_name);
34
4831
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped? (code should work smoothly across different machine.) Thanks, Ann
8
2521
by: hobbes_7_8 | last post by:
Hi everybody! This is basically a pre-processor doubt. I have this very simple define: #ifdef NDEBUG #define QTRACE // #else #define QTRACE qDebug() #endif
13
9411
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could show me the actual source code of assert() function in assert.h header file. I need to write the assert function that prints out error message when the expression is wrong. For eg.,
8
2185
by: junky_fellow | last post by:
Hi, Are there any issues if I define a macro as follows: #define MY_MACRO(a,b); my_function((a),(b)); and calling this macro as follows, MY_MACRO(a,b);
7
1742
by: Paul | last post by:
I would like to define a list of parameters, and then pass them to a macro. However, the compiler (gcc) only sees one parameter, rather than expanding the definition. Could anyone suggest a way of making this work (using the preprocessor)? #define MyList 1,2,3,4 #define Sum(a,b,c,d) a+b+c+d x = Sum(MyList);
5
13482
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8688
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...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
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,...
1
6115
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
5570
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
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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
2
1496
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.