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;
19
  • 3
    This isn't standard C++ is it? I'm pretty sure you can't have special characters in any identifier name.
    – Rapptz
    Commented Jan 30, 2013 at 1:42
  • 2
    What compiler/toolchain?
    – Carl Norum
    Commented Jan 30, 2013 at 1:43
  • @Rapptz, armcc (at least) allows $ in identifiers. So does clang, in a test here.
    – Carl Norum
    Commented Jan 30, 2013 at 1:44
  • 5
    It's too bad. I like the ring of std::put$ and std::get$ better than std::put/get_money.
    – chris
    Commented Jan 30, 2013 at 1:45
  • 2
    @Code-Guru- The C++ standard does not allow $ in identifier names. Commented Jan 30, 2013 at 1:45

4 Answers 4

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.

1
  • 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
    Commented Feb 1, 2013 at 1: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.

2
  • 2
    And the code OP is talking about is from 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
    – user405725
    Commented Jan 30, 2013 at 2:10
  • Suppose the $ sign is mapped it a quotation mark. What would happen then?
    – autistic
    Commented Jan 30, 2013 at 2: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?

1
  • 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
    Commented Feb 1, 2013 at 1: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 ($).

21
  • 2
    FWIW, C++ allows a lot more than these characters in identifiers. Commented Jan 30, 2013 at 1:49
  • 1
    @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. Commented Jan 30, 2013 at 2:20
  • 1
    This is all irrelevant. The section that talks about identifiers is §2.11. Commented Jan 30, 2013 at 2:25
  • 2
    "Consider in Britain, where the pound symbol (£ or ₤) is used in place of the dollar symbol ($)." The logic here is just astounding. Commented Jan 30, 2013 at 2:26
  • 2
    @modifiablelvalue People are trying to help. You scold them? What are you here for, then?
    – sehe
    Commented Jan 30, 2013 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.