'$' character (and others) within identifiers

Discussion in 'C++' started by Jacek Dziedzic, Oct 22, 2006.

  1. Hello!

    I am working on a code which features the following macros:

    #define myname \
    ((std::string)(abi::__cxa_demangle(typeid(myself).name(),\
    0,0,&(Framework::demangle_status))))

    #define $$ \
    ((std::string) "class: " + myname + "\nfunction: " + \
    (std::string) __PRETTY_FUNCTION__ ),

    These are then used within throw statements like this:

    if(!out) logged throw EIOWriteError($$ filename);

    The purpose of all this is to facilitate passing the
    name of the offending class method to the catch() clause
    later on, without having to type it manually within the
    throw part. ("logged" evaluates to whitespace if exception
    logging is off and to some log-writing magic if exception
    logging is on).

    Anyway, the '$' character was chosen for the job as
    it is unlikely to collide with anything in the source
    code (I know, macros are evil). Everything worked fine
    until I tried the "-ansi" compiler switch. I was surprised
    to see that the compiler complains about the '$' character.
    The intel compiler produced an error "expected an identifier"
    on the #define line. g++ does not complain in "-ansi" mode,
    only after adding "-pedantic" it produces a warning
    "$ in identifier or number" on the #define line.

    This is surprising me -- shouldn't the preprocessor
    have already replaced all occurrences of "$$" with the
    macro they represent?

    Which compiler behaves in a Standard-compliant manner?

    Anyway, can someone tell me what exactly the Standard
    says on what characters are allowed in the source code,
    both at the before-preprocessor and after-preprocessor
    level? I thought you could have anything in your source
    files, provided that the preprocessor then replaces
    it with something reasonable...

    TIA,
    - J.
     
    Jacek Dziedzic, Oct 22, 2006
    #1
    1. Advertising

  2. Jacek Dziedzic wrote:
    >
    > Hello!
    >
    > I am working on a code which features the following macros:
    >
    > #define myname \
    > ((std::string)(abi::__cxa_demangle(typeid(myself).name(),\
    > 0,0,&(Framework::demangle_status))))
    >
    > #define $$ \
    > ((std::string) "class: " + myname + "\nfunction: " + \
    > (std::string) __PRETTY_FUNCTION__ ),
    >
    > These are then used within throw statements like this:
    >
    > if(!out) logged throw EIOWriteError($$ filename);
    >
    > The purpose of all this is to facilitate passing the
    > name of the offending class method to the catch() clause
    > later on, without having to type it manually within the
    > throw part. ("logged" evaluates to whitespace if exception
    > logging is off and to some log-writing magic if exception
    > logging is on).
    >
    > Anyway, the '$' character was chosen for the job as
    > it is unlikely to collide with anything in the source
    > code (I know, macros are evil). Everything worked fine
    > until I tried the "-ansi" compiler switch. I was surprised
    > to see that the compiler complains about the '$' character.
    > The intel compiler produced an error "expected an identifier"
    > on the #define line. g++ does not complain in "-ansi" mode,
    > only after adding "-pedantic" it produces a warning
    > "$ in identifier or number" on the #define line.
    >
    > This is surprising me -- shouldn't the preprocessor
    > have already replaced all occurrences of "$$" with the
    > macro they represent?
    >
    > Which compiler behaves in a Standard-compliant manner?
    >
    > Anyway, can someone tell me what exactly the Standard
    > says on what characters are allowed in the source code,
    > both at the before-preprocessor and after-preprocessor
    > level? I thought you could have anything in your source
    > files, provided that the preprocessor then replaces
    > it with something reasonable...


    From 2.2 1:
    ----
    The basic source character set consists of 96 characters: the space
    character, the control characters representing horizontal tab, vertical
    tab, form feed, and new-line, plus the following 91 graphical characters)
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    0 1 2 3 4 5 6 7 8 9
    _ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’
    ----

    $ is obviously not one of them. So, no compiler is required to allow '$'
    in identifier names.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 22, 2006
    #2
    1. Advertising

  3. Clark S. Cox III wrote:
    > From 2.2 1:
    > ----
    > The basic source character set consists of 96 characters: the space
    > character, the control characters representing horizontal tab, vertical
    > tab, form feed, and new-line, plus the following 91 graphical characters)
    > a b c d e f g h i j k l m n o p q r s t u v w x y z
    > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    > 0 1 2 3 4 5 6 7 8 9
    > _ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’
    > ----
    >
    > $ is obviously not one of them. So, no compiler is required to allow '$'
    > in identifier names.


    Thank you. But is the first argument to #define an identifier?
    I assumed that the preprocessor merely performs a textual
    replacement of its first argument by its second argument,
    and that the test for invalid characters only happens
    _after_ this replacement. Looks like I was wrong.

    thanks,
    - J.
     
    Jacek Dziedzic, Oct 22, 2006
    #3
  4. Jacek Dziedzic

    Ron Natalie Guest

    Clark S. Cox III wrote:

    > $ is obviously not one of them. So, no compiler is required to allow '$'
    > in identifier names.
    >
    >

    Of the basic ASCII printables, @ and $ are never used in C or C++
    (outside of string/char literals).
     
    Ron Natalie, Oct 22, 2006
    #4
  5. Ron Natalie wrote:
    > Clark S. Cox III wrote:
    >
    >> $ is obviously not one of them. So, no compiler is required to allow '$'
    >> in identifier names.
    >>
    >>

    > Of the basic ASCII printables, @ and $ are never used in C or C++
    > (outside of string/char literals).


    Yes, but I'm confused as to what your point was, or why this was in
    response to my post.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 22, 2006
    #5
  6. Jacek Dziedzic

    Ron Natalie Guest

    Clark S. Cox III wrote:
    > Ron Natalie wrote:
    >> Clark S. Cox III wrote:
    >>
    >>> $ is obviously not one of them. So, no compiler is required to allow '$'
    >>> in identifier names.
    >>>
    >>>

    >> Of the basic ASCII printables, @ and $ are never used in C or C++
    >> (outside of string/char literals).

    >
    > Yes, but I'm confused as to what your point was, or why this was in
    > response to my post.
    >

    Just adding information, I thought people might find it handy
    to remember.
     
    Ron Natalie, Oct 22, 2006
    #6
  7. Ron Natalie wrote:
    > Clark S. Cox III wrote:
    >
    >> Ron Natalie wrote:
    >>
    >>> Clark S. Cox III wrote:
    >>>
    >>>> $ is obviously not one of them. So, no compiler is required to allow
    >>>> '$'
    >>>> in identifier names.
    >>>>
    >>>>
    >>> Of the basic ASCII printables, @ and $ are never used in C or C++
    >>> (outside of string/char literals).

    >>
    >>
    >> Yes, but I'm confused as to what your point was, or why this was in
    >> response to my post.
    >>

    > Just adding information, I thought people might find it handy
    > to remember.


    Yes, in fact that was the reason for selecting '$' as
    the character used in this macro-magic. As for '@'
    I usually tag all debugs and makeshifts with
    // @@@
    so that it's easy to grep them out long after I forget
    about them.

    - J.
     
    Jacek Dziedzic, Oct 22, 2006
    #7
  8. Jacek Dziedzic

    Jack Klein Guest

    On Sun, 22 Oct 2006 14:56:49 +0200, Jacek Dziedzic
    <jacek@no_spam.tygrys.no_spam.net> wrote in comp.lang.c++:

    > Clark S. Cox III wrote:
    > > From 2.2 1:
    > > ----
    > > The basic source character set consists of 96 characters: the space
    > > character, the control characters representing horizontal tab, vertical
    > > tab, form feed, and new-line, plus the following 91 graphical characters)
    > > a b c d e f g h i j k l m n o p q r s t u v w x y z
    > > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    > > 0 1 2 3 4 5 6 7 8 9
    > > _ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’
    > > ----
    > >
    > > $ is obviously not one of them. So, no compiler is required to allow '$'
    > > in identifier names.

    >
    > Thank you. But is the first argument to #define an identifier?
    > I assumed that the preprocessor merely performs a textual
    > replacement of its first argument by its second argument,
    > and that the test for invalid characters only happens
    > _after_ this replacement. Looks like I was wrong.


    Yes, I'm afraid you were. The first operand to #define is an
    identifier, and it has exactly the same characteristics, requirements,
    and limitations as any other identifier.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 23, 2006
    #8
    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. erikcw
    Replies:
    21
    Views:
    531
    Arnaud Delobelle
    Feb 8, 2008
  2. Replies:
    1
    Views:
    360
    Roedy Green
    Apr 22, 2008
  3. Replies:
    9
    Views:
    306
  4. Francesco
    Replies:
    4
    Views:
    939
    Francesco
    Sep 7, 2009
  5. Travis
    Replies:
    3
    Views:
    381
Loading...

Share This Page