'$' character (and others) within identifiers

J

Jacek Dziedzic

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.
 
C

Clark S. Cox III

Jacek said:
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
_ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’
 
J

Jacek Dziedzic

Clark said:
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
_ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’

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.
 
R

Ron Natalie

Clark said:
$ 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).
 
C

Clark S. Cox III

Ron said:
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.
 
R

Ron Natalie

Clark said:
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.
 
J

Jacek Dziedzic

Ron said:
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.
 
J

Jack Klein

Clark said:
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
_ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ~ ! = , \ " ’

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top