9

I read the following code from an open source library. What confuses me is the usage of dollar sign. Can anyone please clarify the meaning of $ in the code. Your help is greatly appreciated!

   __forceinline MutexActive( void ) : $lock(LOCK_IS_FREE) {}
    void lock  ( void );
    __forceinline void unlock( void ) { 
      __memory_barrier();     // compiler must not schedule loads and stores around this point
      $lock = LOCK_IS_FREE; 
    }
  protected:
    enum ${ LOCK_IS_FREE = 0, LOCK_IS_TAKEN = 1 };
    Atomic $lock;
James
  • 89
  • 1
  • 1
  • 4
  • 3
    This isn't standard C++ is it? I'm pretty sure you can't have special characters in any identifier name. – Rapptz Jan 30 '13 at 01:42
  • 2
    What compiler/toolchain? – Carl Norum Jan 30 '13 at 01:43
  • @Rapptz, `armcc` (at least) allows `$` in identifiers. So does `clang`, in a test here. – Carl Norum Jan 30 '13 at 01:44
  • `$` is allowed in an identifier name in C++ – Code-Apprentice Jan 30 '13 at 01:44
  • dup: http://stackoverflow.com/questions/936744/are-dollar-signs-allowed-in-identifiers-in-c – thang Jan 30 '13 at 01:44
  • I'm pretty certain `$` is not part of the STANDARD characters set, but some compilers do indeed allow it to be used in the same way as `_` - that is, it's counted as a "letter" just like `A-Za-z_` – Mats Petersson Jan 30 '13 at 01:44
  • 4
    It's too bad. I like the ring of `std::put$` and `std::get$` better than `std::put/get_money`. – chris Jan 30 '13 at 01:45
  • 2
    @Code-Guru- The C++ standard does not allow $ in identifier names. – templatetypedef Jan 30 '13 at 01:45
  • @MatsPetersson: The C++ and C standards explicitly allow underscores though – dreamlax Jan 30 '13 at 01:47
  • @dreamlax, Yes, but don't go giving ideas, what with the reservations attached to underscores that are not present with anything else ;) – chris Jan 30 '13 at 01:48
  • The preprocessor lets you `#define` '$'. Try to find out where it's defined and you'll understand what's going on. – Cameron Jan 30 '13 at 01:49
  • There must be some replacement of $ with another string. The '$' tokens don't make sense for C++. Most likely some preprocessing step is going to replace all instances of $lock with some real name. The part that makes no sense is the $ in the enum. – doug65536 Jan 30 '13 at 01:49
  • I agree with @doug65536, it seems `$` has a special meaning here, perhaps something is being inserted by another "preprocessor". – s.bandara Jan 30 '13 at 01:51
  • @chris: It's only reserved if it starts with an underscore and is followed by an uppercase letter or another underscore, e.g. `_Reserved`, `__reserved`, etc. – dreamlax Jan 30 '13 at 01:51
  • Yes, I meant that SOME compilers allow $ in addition to the regular characters that count as "letters" in symbols, of which _ is one. – Mats Petersson Jan 30 '13 at 01:53
  • gcc has a switch: -fdollars-in-identifiers allowing $ in identifiers. – doug65536 Jan 30 '13 at 01:59
  • @templatetypedef: By what logic? – Lightness Races in Orbit Jan 30 '13 at 02:15
  • Only @ (at), ` (backtick) and $ (dollar sign) are not used by C++. Every other symbol on an US keyboard is used. I think it was wise to leave a few characters out. – doug65536 Jan 30 '13 at 02:28
  • @dreamlax, Or a global identifier starting with an underscore, yes, and the double underscore can be anywhere in the name for it to be reserved. – chris Jan 30 '13 at 02:42

4 Answers4

10

There is a gcc switch, -fdollars-in-identifiers which explicitly allows $ in idenfitiers.

Perhaps they enable it and use the $ as something that is highly unlikely to clash with normal names.

-fdollars-in-identifiers

Accept $ in identifiers. You can also explicitly prohibit use of $ with the option -fno-dollars-in-identifiers. (GNU C allows $ by default on most target systems, but there are a few exceptions.) Traditional C allowed the character $ to form part of identifiers. However, ISO C and C++ forbid $ in identifiers.

See the gcc documentation. Hopefully the link stays good.

Community
  • 1
  • 1
doug65536
  • 6,562
  • 3
  • 43
  • 53
  • This project (embree) is from Intel, and they tested in Intel compiler, gcc and Microsoft Compiler on Linux, Mac and Windows system. so gcc seems to support the dollar symbol – James Feb 01 '13 at 01:06
9

It is being used as part of an identifer.

[C++11: 2.11/1] defines an identifier as "an arbitrarily long sequence of letters and digits." It defines "letters and digits" in a grammar given immediately above, which names only numeric digits, lower- and upper-case roman letters, and the underscore character explicitly, but does also allow "other implementation-defined characters", of which this is presumably one.

In this scenario the $ has no special meaning other than as part of an identifier — in this case, the name of a variable. There is no special significance with it being at the start of the variable name.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    And the code OP is talking about is from https://code.google.com/p/point-frag/ in case anybody is interested. And the author is using `-std=c++0x` flag in CMakeLists.txt. So you are right. +1 –  Jan 30 '13 at 02:10
  • Suppose the $ sign is mapped it a quotation mark. What would happen then? – autistic Jan 30 '13 at 02:47
0

Even if dollar sign are not valid identifiers according to the standard, it can be accepted. For example visual studio (I think ggc too but I'm not sure about that) seems to accept it.

Check this doc : http://msdn.microsoft.com/en-us/library/565w213d(v=vs.80).aspx and this : Are dollar-signs allowed in identifiers in C++03?

Community
  • 1
  • 1
Bdloul
  • 882
  • 10
  • 27
  • I can confirm this as I am opening the source code in VS2008 and it compiles OK, As this project (embree) is from Intel, and they tested in Intel compiler, gcc and Microsoft Compiler on Linux, Mac and Windows system. I would say the three compilers all support $ symbol as part of an identifier now – James Feb 01 '13 at 01:04
-1

The C++ standard says:

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

There is no $ in the basic source character set described above; The $ character in your code is an extension to the basic source character set, which isn't required. Consider in Britain, where the pound symbol (£ or ₤) is used in place of the dollar symbol ($).

autistic
  • 1
  • 3
  • 35
  • 80
  • 2
    FWIW, C++ allows a lot more than these characters in identifiers. – R. Martinho Fernandes Jan 30 '13 at 01:49
  • @R.MartinhoFernandes A valid C++ implementation need not tolerate any characters except for those specified above when parsing source code. – autistic Jan 30 '13 at 01:57
  • While correct, that does not make the example's use of $ illegal, so it's irrelevant. – R. Martinho Fernandes Jan 30 '13 at 01:57
  • Using the $ character forbids the example from being strictly-compliant C++ code. A valid implementation may raise a constraint violation message in response to the inclusion of the $ character in a source file. In that case, the meaning of $ is "syntax error". – autistic Jan 30 '13 at 02:03
  • @modifiablelvalue: Why do you say that? Can you demonstrate that with a quote from the standard? You haven't done that yet. – Lightness Races in Orbit Jan 30 '13 at 02:17
  • @Non-StopTimeTravel Translation phase 1: Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary. The set of physical source file characters accepted is implementation-defined. – autistic Jan 30 '13 at 02:18
  • @modifiablelvalue: The rest of that paragraph says `Any source file character not in the basic source character set (2.3) is replaced by the universal-character-name that designates that character. (An implementation may use any internal encoding, so long as an actual extended character encountered in the source file, and the same extended character expressed in the source file as a universal-character-name (i.e., using the \uXXXX notation), are handled equivalently except where this replacement is reverted in a raw string literal.)` You should read _all_ of it. – Lightness Races in Orbit Jan 30 '13 at 02:20
  • In addition to my answer. If a physical source file character $ appears and an implementation has no translation to a basic source file character, then it may raise a diagnostic message. – autistic Jan 30 '13 at 02:20
  • @Non-StopTimeTravel That is under the provision that the physical source file character $ is accepted. – autistic Jan 30 '13 at 02:21
  • Also pay attention to the phrase "if necessary". You're reading selectively from the standard, which does not work. – Lightness Races in Orbit Jan 30 '13 at 02:21
  • @modifiablelvalue: No, it's not. It's an explicit provision that any source file character not in the basic source character set will simply end up being a `universal-character-name`. – Lightness Races in Orbit Jan 30 '13 at 02:21
  • 1
    This is all irrelevant. The section that talks about identifiers is §2.11. – R. Martinho Fernandes Jan 30 '13 at 02:25
  • @Non-StopTimeTravel To clarify, there is a physical source file character set. The $ sign need not be in this character set. If it isn't, then the implementation may raise a diagnostic when attempting to translate it to a source file character. – autistic Jan 30 '13 at 02:26
  • 2
    "Consider in Britain, where the pound symbol (£ or ₤) is used in place of the dollar symbol ($)." The logic here is just astounding. – Cat Plus Plus Jan 30 '13 at 02:26
  • @modifiablelvalue: I already quoted, from the same passage, the phrasing that completely defies that logic. The only way you can clarify this is to reverse your argument. – Lightness Races in Orbit Jan 30 '13 at 02:27
  • @R.MartinhoFernandes ... and no $ sign is mentioned in the whitespace section, either. Are you going to quote the relevant section for that, too? – autistic Jan 30 '13 at 02:30
  • Yeah god guys stop quoting the _standard_ what will that ever accomplish – Lightness Races in Orbit Jan 30 '13 at 02:32
  • 2
    @modifiablelvalue People are trying to help. You scold them? What are you here for, then? – sehe Jan 30 '13 at 02:33
  • sehe: I don't see how taking one of the possible implementation-defined avenues that "implementation-defined" covers and attempting to dismiss my point regarding implementation-defined behaviour is helpful. Sure, an implementation may or may not choose to define $ as an identifier symbol, as it may or may not choose to define $ as a whitespace symbol, or an arithmetic operator, or a syntax error. – autistic Jan 30 '13 at 02:38
  • @Non-StopTimeTravel: What will using a dictionary accomplish? – autistic Jan 30 '13 at 02:44
  • @sehe: _(notification ping)_ – Lightness Races in Orbit Jan 30 '13 at 02:50