Preprocessor: convert __LINE__ to const char*

Discussion in 'C++' started by Torsten Wiebesiek, Aug 29, 2008.

  1. 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
     
    Torsten Wiebesiek, Aug 29, 2008
    #1
    1. Advertising

  2. 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.
     
    Marco Manfredini, Aug 29, 2008
    #2
    1. Advertising

  3. Torsten Wiebesiek, Aug 29, 2008
    #3
  4. >> 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. :)
     
    Torsten Wiebesiek, Aug 29, 2008
    #4
  5. Torsten Wiebesiek

    red floyd Guest

    On Aug 29, 7:02 am, Torsten Wiebesiek <-hannover.de>
    wrote:
    > Just found the solution at an unexpected place :)http://en.wikipedia.org/wiki/C_preprocessor#Indirectly_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()).
     
    red floyd, Aug 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,555
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,160
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    906
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,153
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    625
    James Kanze
    Sep 4, 2007
Loading...

Share This Page