| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jacek Dziedzic" |
| Date: |
22 Oct 2006 06:48:51 AM |
| Object: |
'$' character (and others) within identifiers |
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.
.
|
|
| User: "Clark S. Cox III" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 07:47:07 AM |
|
|
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
clarkcox3@gmail.com
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 10:39:07 AM |
|
|
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).
.
|
|
|
| User: "Clark S. Cox III" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 12:53:56 PM |
|
|
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
clarkcox3@gmail.com
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 01:55:48 PM |
|
|
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.
.
|
|
|
| User: "Jacek Dziedzic" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 02:55:12 PM |
|
|
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.
.
|
|
|
|
|
|
|
| User: "Jacek Dziedzic" |
|
| Title: Re: '$' character (and others) within identifiers |
22 Oct 2006 07:56:49 AM |
|
|
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 character=
s)
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
_ { } [ ] # ( ) < > % : ; . ? * + - / =CB=86 & | ~ ! =3D , \ " =E2=80=99=
----
=20
$ 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.
.
|
|
|
| User: "Jack Klein" |
|
| Title: Re: '$' character (and others) within identifiers |
23 Oct 2006 03:52:38 PM |
|
|
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
.
|
|
|
|
|
|

|
Related Articles |
|
|