473,699 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PREPROCESSOR: Multiple expansions.

(This is a contrived example)

With the following preprocessor lines:

#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) \
OUT1(SIZEOF_ ## TYPE, VAR)

#define OUT1(SIZE, VAR) \
hton ## SIZE ## b(VAR)

I type:

OUT(mytype, t)

and want to get:

htonl(t)

but actually get:

htonSIZEOF_myty peb(t)

I tried various intermediate macros to catenate SIZEOF_ and TYPE to try
get it to convert to 4 but the closest I have gotten is:

hton 4 b(t)

Whats the trick?

Thanks.

Aug 21 '06 #1
4 1650
well, you're pushing the non-barfing limits of macro usage, but, ok,

does it work any better if you take out the spaces around the ##'s?

Aug 21 '06 #2

Ancient_Hacker wrote:
well, you're pushing the non-barfing limits of macro usage, but, ok,

does it work any better if you take out the spaces around the ##'s?
I did as it happens (spaces surrounding glue have no effect as far as I
can see).

Calm prevailed and I went through it slowly and carefully, resulting in
the following which behaves as expected.

#define SIZEOF_mytype 4

#define hton4b htonl

#define CAT(A, B) A##B

#define OUT2(ARG, VAR) \
CAT(hton, ARG)

#define OUT1(SIZE, VAR) \
OUT2(CAT(SIZE,b (VAR)), VAR)

#define OUT(TYPE, VAR) \
OUT1(SIZEOF_##T YPE, VAR)

(Incidentally, the result is the same when using "A ## B" and "SIZEOF_
## TYPE").

Thanks for the reply anyway.

Aug 21 '06 #3
This works:
#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) OUT1(SIZEOF_##T YPE, VAR)

#define OUT1(SIZE,VAR) expand(hton,SIZ E,VAR)

#define expand(x,y,z) x##y##b(z)
OUT(mytype, t)

Aug 21 '06 #4

Ancient_Hacker wrote:
This works:
#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) OUT1(SIZEOF_##T YPE, VAR)

#define OUT1(SIZE,VAR) expand(hton,SIZ E,VAR)

#define expand(x,y,z) x##y##b(z)
OUT(mytype, t)
Thanks.

Aug 21 '06 #5

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

Similar topics

205
10639
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
13
3583
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the amount of preprocessor directives used in these code. Can anyone please tell me how to actually use these preprocessor directives in a more professional way like selecting particular lines of code suited for some particular hardware etc...
13
2128
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int defined = 2;
2
1619
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We now compile file1.c , make it's object file. and use this object file in the other file i.e file2.c. Then can we use the macro TEST1 in file2.c as well. As I understand about macros, they are replaced by their corresponding values , but are the...
0
1056
by: robert | last post by:
I installed the C# snippets that were recently made available by Microsoft. Since then when I right-click and choose either Insert Snippet or Surround With... or press ctrl space, choose a snippet and press tab tab I get Multiple Snippets: <choices> I have all my snippets in the default location and nowhere else. The IDE must be reading old values from somewhere. I can't get rid of them. I have tried the registry entries in HKCU and HKLM...
6
2427
by: Urs Thuermann | last post by:
Does a tool exist to apply C preprocessor expansion to a C source only partially, i.e. replace only some directives? I want to replace some #if's by their expansions, e.g. all #ifdef SOME_SYMBOL, but keep all other #ifdef OTHER_SYMBOL and all #include directives. urs
32
2784
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger differences ? If the output is not deterministic then is it possible that the output of the...
4
1272
by: David W | last post by:
In VS .NET 2003 (SP1) I wanted to add a preprocessor symbol to all configurations of all projects in a solution. I selected all the projects and then went to Properties, where I went to the preprocessor symbols edit box. Nothing was in there, presumably because no symbols are common to all selections, which is as expected. However, when I added the new symbol I found that _all_ other symbols, such as _WINDOWS etc. had been deleted from all...
31
2914
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
0
8705
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
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9197
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
9054
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
8941
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
7785
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
6549
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...
1
3071
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
2015
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.