2

I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has characteristics similar to this in the corresponding header files:

extern "C" {
    #define HELLO__THERE 1
    int hello__out__there( int );
}

I will be using this header in a C++ project, and plan to be doing things in C++ like:

if (HELLO__THERE == abc) 
    hello__out__there(foo);

Is this acceptable behavior in C++, covered by the standard?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
test
  • 411
  • 1
  • 4
  • 10
  • Why do you want to use a `#define` instead of a `const`? – Etienne de Martel Jul 20 '11 at 01:38
  • @Etienne yeah... uh this is just some sample code to help explain the issue. The real code is nothing like what I posted. Basically I have a lot of define macros and functions with double underlines. – test Jul 20 '11 at 01:51
  • If you have full control over the code, you should change the code so that you do not use reserved identifiers. If you do not have full control over the code, then you gotta do what you gotta do. – James McNellis Jul 20 '11 at 02:37
  • What you describe is definitely covered by the standard. Please define what you mean by acceptable. – Martin York Jul 20 '11 at 02:42

4 Answers4

12

In the C++03 standard 17.4.3.1.2 Global names, that use of underscores is defined as reserved:

Each name that contains a double underscore (_ _) or begins with an underscore followed by an upper- case letter (2.11) is reserved to the implementation for any use.

Being reserved means that it might be used in any conforming implementation and therefore it is not advisable to use it.

lccarrasco
  • 2,031
  • 1
  • 17
  • 20
2

You should be fine, unless by some fluke chance that one of the defines has clashes with your compiler's one. If that is the case, it'll likely be a warning or error (depending on your compiler's configuration) that there'll be a duplicate symbol.

Hope it helps. Cheers!

Vern
  • 2,393
  • 1
  • 15
  • 18
0

The method call would be OK but why compare HELLO_THERE to some value abc? If you were testing to see if a method was there I would wrap it in #ifdef ... #endif instead because if hello_out_there is not defined for some reason that would be a compile error.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
-11

double underlines in identifiers are reserved for the compiler

First, it's underscore I guess. Second such identifiers are reserved. That doesn't hold one back to not use it. You can use it (until there is no naming conflict).

Is this acceptable behavior in C++, covered by the standard?

Yes. It's acceptable. However, there is difference between acceptable and good code. If you are following a proper coding guidelines then your code will be good as well as acceptable. IMHO, you should refer to some good coding standards on internet; it will help you a lot.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 4
    Please read this before deciding what is correct: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797 – Martin York Jul 20 '11 at 02:13
  • @Martin, OP has clearly mentioned: `I understand that in C++ double underscores in identifiers are reserved for the compiler ... Is this acceptable behavior in C++`. OP is aware of what he/she is doing. I have also mentioned in my answer, `until there is no naming conflict ...` and about good coding standards. If a programmer is ok with putting `__` (knowing the compiler facts) then why someone should stop it ? I think you are developing a habit to downvote my any accepted answers. – iammilind Jul 20 '11 at 02:22
  • 3
    You are getting paranoid if you think I down voted you. I reserve my down-votes for bad code. Otherwise I find a comment is usually enough to persuades people to clean up their answer (you seem to stubbornly leave bad answers lying around though) so that it is at acceptably high quality that we expect on SO. I have no grudge no opinion and carry nothing forward. I comment on each question based on its validity. – Martin York Jul 20 '11 at 02:25
  • 1
    "If a programmer is ok with [doing something that is inadvisable and incorrect], then why should someone stop it?" That is a silly question, no? – James McNellis Jul 20 '11 at 02:35
  • @All, I am retaining with my answer with small edit, putting `until there is no naming conflict` in **bold**. I am not advising to use it, which is apparent from my answer. I am just saying whether it's legal or not (because OP has asked in the same context; OP knows that compiler reserve `__`). If you feel my **answer is wrong in OP's question context**; please downvote. – iammilind Jul 20 '11 at 02:58
  • 3
    "I understand that the law forbids me from going 100 mph on the freeway. I just drove along at 120 mph. Is this acceptable driving behavior, covered by the law?" What is the difference between this and the OP? The OP is asking if code explicitly marked as ill-formed is acceptable. It isn't. Intentionally invoking UB is rarely an acceptable coding practice. In this case, that UB is easily avoided. – David Hammen Jul 20 '11 at 03:17
  • @iammilind IMO If this was simply an issue of personal style it wouldn't be an problem. Unfortunately that is not the case. You are suggesting to the OP that ignoring the rules of a reserved construct is acceptable. It's not. It does nothing but foster bad coding practices and encourages developers to continue doing things they know they shouldn't do. – Captain Obvlious Jul 20 '11 at 09:14
  • @All ... wow now I'm confused! Perhaps this is my fault for not stating the question clearly enough. I have code from several C projects, none of which are mine, and in those projects there are functions and defines that use double underscores. So I have to use some of these C projects in C++, and being aware of the double underscore prohibition I naturally wonder if it is acceptable to use this C code with a C++ compiler. When I say acceptable I mean is it permitted by the standard for compatibility to call a C function like__this() in C++ code? What about a having a define macro LIKE__THIS ? – test Jul 21 '11 at 00:51