Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Preprocessor: convert __LINE__ to const char*

Reply
Thread Tools

Preprocessor: convert __LINE__ to const char*

 
 
Torsten Wiebesiek
Guest
Posts: n/a
 
      08-29-2008
Hi,

I have a problem with the preprocessor. I have written my own little
assert macro. This is supposed to log a message (with log4cxx):

#define LogAssert(Expression) \
if (Expression) { \
LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
#Expression " in file " __FILE__ ", line " #__LINE__ "."); \
::exit(1); \
}

Unfortunately, the preprocessor is only quoting marcro arguments. In my
case, Expression is quoted with #Expression. __LINE__ expands to an
integer literal. Is there an way to convert __LINE__ to a const char* at
compile time?

Thanks for you help,

Torsten
 
Reply With Quote
 
 
 
 
Marco Manfredini
Guest
Posts: n/a
 
      08-29-2008
Torsten Wiebesiek wrote:

> Hi,
>
> I have a problem with the preprocessor. I have written my own little
> assert macro. This is supposed to log a message (with log4cxx):
>
> #define LogAssert(Expression) \
> if (Expression) { \
> LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
> #Expression " in file " __FILE__ ", line " #__LINE__ "."); \
> ::exit(1); \
> }
>
> Unfortunately, the preprocessor is only quoting marcro arguments. In


So turn the expansion of __LINE__ into a macro argument.
#define STR2(X) #X
#define STR(X) STR2(X)
#define LOG(X) logger(X " happend here:" __FILE__ STR(__LINE__))

STR(X) expands __LINE__ to the actual line number, and STR2 does the
quoting.

 
Reply With Quote
 
 
 
 
Torsten Wiebesiek
Guest
Posts: n/a
 
      08-29-2008
Just found the solution at an unexpected place
http://en.wikipedia.org/wiki/C_prepr...acro_arguments

1: #define _QUOTE(x) #x
2: #define QUOTE(x) _QUOTE(x)
3:
4: _QUOTE(__LINE__) // expands to "__LINE__"
5: QUOTE(__LINE__) // expands to "5"
 
Reply With Quote
 
Torsten Wiebesiek
Guest
Posts: n/a
 
      08-29-2008
>> I have a problem with the preprocessor. I have written my own little
>> assert macro. This is supposed to log a message (with log4cxx):
>>
>> #define LogAssert(Expression) \
>> if (Expression) { \
>> LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
>> #Expression " in file " __FILE__ ", line " #__LINE__ "."); \
>> ::exit(1); \
>> }
>>
>> Unfortunately, the preprocessor is only quoting marcro arguments. In

>
> So turn the expansion of __LINE__ into a macro argument.
> #define STR2(X) #X
> #define STR(X) STR2(X)
> #define LOG(X) logger(X " happend here:" __FILE__ STR(__LINE__))
>
> STR(X) expands __LINE__ to the actual line number, and STR2 does the
> quoting.


Thanks, a lot and have a nice weekend.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 7:02*am, Torsten Wiebesiek <(E-Mail Removed)-hannover.de>
wrote:
> Just found the solution at an unexpected place http://en.wikipedia.org/wiki/C_prepr...quoting_macro_...
>
> 1: #define _QUOTE(x) *#x
> 2: #define QUOTE(x) *_QUOTE(x)
> 3:
> 4: _QUOTE(__LINE__) * *// expands to "__LINE__"
> 5: QUOTE(__LINE__) * * // expands to "5"


This is bad. Identifiers with leading underscore followed by an
uppercase letter are reserved to the implementation. That is, your
implementation may be using _QUOTE for its own purposes.

Either use a trailing underscore, or some other convention (i.e.
QUOTE_() or QUOTE2()).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments