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 4 8335
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.
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
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...
Thanks guys - those all worked. That's what I call service!
Dom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
...
|
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...
|
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)
|
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 )
|
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)...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
| |