Gili
Gili

Reputation: 90101

Are dollar-signs allowed in identifiers in C++03?

What does the C++ standard say about using dollar signs in identifiers, such as Hello$World? Are they legal?

Upvotes: 16

Views: 10715

Answers (7)

Arne Mertz
Arne Mertz

Reputation: 24626

In C++03, the answers given earlier are correct: they are illegal. In C++11 the situation changed however:

The answer here is "Maybe":
According to §2.11, identifiers may consist of digits and identifier-nondigits, starting with one of the latter. identifier-nondigits are the usual a-z, A-Z and underscore, in addition since C++11 they include universal-character-names (e.g. \uBEAF, \UC0FFEE32), and other implementation-defined characters. So it is implementation defined if using $ in an identifier is allowed. VC10 and up supports that, maybe earlier versions, too. It even supports identifiers like こんばんは.

But: I wouldn't use them. Make identifiers as readable and portable as possible. $ is implementation defined and thus not portable.

Upvotes: 14

MSalters
MSalters

Reputation: 179991

The relevant section is "2.8 Identifiers [lex.name]". From the basic character set, the only valid characters are A-Z a-z 0-9 and _. However, characters like é (U+00E9) are also allowed. Depending on your compiler, you might need to enter é as \u00e9, though.

Upvotes: 1

Kevin Loney
Kevin Loney

Reputation: 7553

A c++ identifier can be composed of any of the following: _ (underscore), the digits 0-9, the letters a-z (both upper and lower case) and cannot start with a number.

There are a number of exceptions as C99 allows extensions to the standard (e.g. visual studio).

Upvotes: 16

Nathaniel Flath
Nathaniel Flath

Reputation: 16025

They are illegal. The only legal characters in identifiers are letters, numbers, and _. Identifiers also cannot start with numbers.

Upvotes: 14

Artyom
Artyom

Reputation: 31273

Not legal, but many if not most of compilers support them, note this may depend on platform, thus gcc on arm does not support them due to assembly restrictions.

Upvotes: 8

David Thornley
David Thornley

Reputation: 57046

Illegal. I think the dollar sign and backtick are the only punctuation marks on my keyboard that aren't used in C++ somewhere (the "%" sign is in format strings, which are in C++ by reference to the C standard).

Upvotes: 0

Zifre
Zifre

Reputation: 27008

They are not legal in C++. However some C/C++ derived languages (such as Java and JavaScript) do allow them.

Upvotes: 0

Related Questions