473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macro expansion of '#__LINE__'?

Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

....

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?

Thanks -

Dom
Nov 4 '05 #1
4 8369
On 2005-11-04, Dom Gilligan <dg****@hotmail .com> wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.


#define STRING2(x) #x
#define STRING(x) STRING2(x)
#define STR_LINE STRING(__LINE__ )

the stringize operator is weird, but this seems to work.
Nov 4 '05 #2
Dom Gilligan wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }


Arguments of '#' are not considered for further macro replacement. That's why
your __LINE__ is not replaced with the line number. You need an extra macro
level to give __LINE__ an opportunity to get replaced

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) OP3(x, y, z)
#define OP3(x,y,z) { x ", line " #y ": ", z }

--
Best regards,
Andrey Tarasevich
Nov 4 '05 #3
On Fri, 04 Nov 2005 17:09:53 +0000, Dom Gilligan wrote:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.

Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:

---------------

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " #y ": ", z }
struct opinfo {
const char *preamble;
const char *cstr;
};

...

opinfo ops[] = {
OP("wibble1"),
OP("wibble2")
};

---------------

My intention is that this should produce an array of two opinfos which
look like:

opinfo ops[] = {
{ "src.cc, line 10: ", "wibble1" },
{ "src.cc, line 11: ", "wibble2" }
};

However, what it actually produces is:

opinfo ops[] = {
{ "src.cc, line __LINE__: ", "wibble1" },
{ "src.cc, line __LINE__: ", "wibble2" }
};

Any ideas?


You need to stringize indirectly:

#define OP(z) OP2(__FILE__, __LINE__, z)
#define OP2(x,y,z) { x ", line " STR(y) ": ", z }
#define STR(a) # a

Victor

P.S. Sorry for the extensive quoting...
Nov 4 '05 #4
Thanks guys - those all worked. That's what I call service!

Dom
Nov 4 '05 #5

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

Similar topics

3
2080
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not python, but the module imported above # will use this block internally and prevent it
3
2544
by: Ark | last post by:
Hello, NG, Please, help on this snippet: #define CAT(a,b) a##b #define COMMENT CAT(/,/) COMMENT This is a comment Should it compile? It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM compiler.
7
3500
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. The Faq pointed me to scpp but i could not compile it. the lex.c generated by flex 2.5.4 is broken.
4
3691
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do crazy things (VFP uses & instead of $): x="sales" sales="1000" salestax="8.25"
2
3495
by: srinu.fsl | last post by:
there's a MACRO call : MACRO1(cnf) and its expansion is : #define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err = SUCCESS)); #define CLEANUP(a)
3
2010
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
0
1213
by: borophyll | last post by:
Hi all Can anyone explain to me the algorithm for macro expansion in the C++ preprocessor. I am confused why the following code works like it does: #define A(x) #x #define B(x) A(x) #define TEST 1
1
3380
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based micro-controllers. So why am I posting herein? Well, the ARMbasic environment makes use of a tool borrowed from your folk's environment - CPP. The build details are: C:\Program Files\Coridium>cpp --version cpp (GCC) 3.2.3 (mingw special 20030504-1) Copyright...
5
1780
by: Srinivas Mudireddy | last post by:
Hi, We have bunch of message levels and depending on whether that level is turned on, messages of that level are printed. For example, we have levels like MSG_LOW, MSG_MED etc. We want to provide a wrapper API on top of this to print various kinds of messages and map the wrapper API to either MSG_LOW or MSG_MED. For example, our wrapper API have macros such as MSG_FUNCTION_ENTRY, MSG_FUNCTION_EXIT, MSG_STATE_CHANGE, MSG_INVALID_INPUT...
0
9680
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
10456
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
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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
7548
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
6788
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.