0

I'm wondering if it is considered bad practice to use the characters $ or @ in C++ code where it is possible, like in macros.

I haven't seen those anywhere, even if they are in the ASCII table, just like # character for example.

I'm actually very tempted to name my macros like this "$SomeMacro()" instead of the good old screaming "SOME_MACRO()", it compiles fine in gcc, clang and msvc.

Nowine
  • 166
  • 8
  • 4
    Use them for what? Have you tried compiling it? – UnholySheep Apr 15 '19 at 18:39
  • 1
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Apr 15 '19 at 18:40
  • 1
    If you mean using them in variable names, then no you cannot. Variables can only use underscores, letters, and numbers. – anonmess Apr 15 '19 at 18:41
  • 1
    if you change "is it bad practice" to "is it allowed" it can make a nice question. The thing is what is bad practice is opinion-based and questions that are primarily opinion-based are considered offtopic at SO – 463035818_is_not_an_ai Apr 15 '19 at 18:45
  • Sorry my question missed a detail, it was mean to be used in macros, I'll edit this – Nowine Apr 15 '19 at 18:55
  • for the opinion-based part: Yes it is bad practice. Variable names should be readable and pronouncable. If your variable has a `$` in it you make it more difficult to talk about it with your collegues for no gain – 463035818_is_not_an_ai Apr 15 '19 at 18:56
  • 2
    offtopic: better not use macros in the first place. In modern c++ there is only very few things you cannot do without macros – 463035818_is_not_an_ai Apr 15 '19 at 19:00

1 Answers1

1

It is not a good idea by any means, but if you come from the world of JavaScript (e.g. the famous jQuery's $) or other languages and like that, and you want to be fancy, you can indeed use a lot of things!

For instance, $ works as an extension in many compilers:

int $() {
    return 42;
}

You can also use other Unicode characters:

int ᚁᚂᚃ() {
    return 42;
}

And, you can even use emoji:

int () {
    return 42;
}

See e.g. Does C++11 allow dollar signs in identifiers? for more formal details.

Also, note that under MSVC you will probably want /utf-8 and /permissive- if you want to play with this.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 3
    it's worth noting that not all toolchains fully support these identifiers in some cases. I know at least one linker that would probably barf on emoji – Mgetz Apr 15 '19 at 18:49
  • @Mgetz I can imagine... :) – Acorn Apr 15 '19 at 18:51
  • 1
    i only found the list at the bottom here https://en.cppreference.com/w/cpp/language/identifiers It has some really weird symbols and now I am a bit confused on what is really allowed and what not – 463035818_is_not_an_ai Apr 15 '19 at 18:51
  • indeed very fancy :) but it was mean to be used in macros, I edited my question – Nowine Apr 15 '19 at 19:00
  • @Acorn just tested on godbolt: GCC: emoji rejected, MSVC: compiles, Clang: Compiles, ICC: Compiles – Mgetz Apr 15 '19 at 19:01
  • Your link led me to this link https://stackoverflow.com/questions/18494262/what-are-the-and-for-in-c-c?noredirect=1&lq=1 which says that those characters are basically not portable. Thanks, I have my answer – Nowine Apr 15 '19 at 19:27
  • @Mgetz GCC should compile it, it is a bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67224 (see e.g. https://stackoverflow.com/questions/12692067/and-other-unicode-characters-in-identifiers-not-allowed-by-g ) – Acorn Apr 15 '19 at 20:51