Connecting Tech Pros Worldwide Forums | Help | Site Map

'$' character (and others) within identifiers

Jacek Dziedzic
Guest
 
Posts: n/a
#1: Oct 22 '06

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.

Clark S. Cox III
Guest
 
Posts: n/a
#2: Oct 22 '06

re: '$' character (and others) within identifiers


Jacek Dziedzic wrote:
Quote:
>
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
clarkcox3@gmail.com
Jacek Dziedzic
Guest
 
Posts: n/a
#3: Oct 22 '06

re: '$' character (and others) within identifiers


Clark S. Cox III wrote:
Quote:
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.
Ron Natalie
Guest
 
Posts: n/a
#4: Oct 22 '06

re: '$' character (and others) within identifiers


Clark S. Cox III wrote:
Quote:
$ 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).
Clark S. Cox III
Guest
 
Posts: n/a
#5: Oct 22 '06

re: '$' character (and others) within identifiers


Ron Natalie wrote:
Quote:
Clark S. Cox III wrote:
>
Quote:
>$ 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
clarkcox3@gmail.com
Ron Natalie
Guest
 
Posts: n/a
#6: Oct 22 '06

re: '$' character (and others) within identifiers


Clark S. Cox III wrote:
Quote:
Ron Natalie wrote:
Quote:
>Clark S. Cox III wrote:
>>
Quote:
>>$ 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.
Jacek Dziedzic
Guest
 
Posts: n/a
#7: Oct 22 '06

re: '$' character (and others) within identifiers


Ron Natalie wrote:
Quote:
Clark S. Cox III wrote:
>
Quote:
>Ron Natalie wrote:
>>
Quote:
>>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.
Jack Klein
Guest
 
Posts: n/a
#8: Oct 23 '06

re: '$' character (and others) within identifiers


On Sun, 22 Oct 2006 14:56:49 +0200, Jacek Dziedzic
<jacek@no_spam.tygrys.no_spam.netwrote in comp.lang.c++:
Quote:
Clark S. Cox III wrote:
Quote:
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/~a...FAQ-acllc.html
Closed Thread