-2

Is there any technical reason for standard library (C or C++) implementations to, IMO abuse, underscores the way they do (=prefix everything with two undescore + add a trailing underscore to denote that a variable is a member variable)?

I get that /.*__.*/ and /_[A-Z].*/ (<= regexes) are reserved-by-implementation. But isn't that supposed to refer to the implementation of the compiler rather than a (standard) library?

Couldn't a standard library behave like any other library in terms of choosing its internal names?

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 1
    It's not an abuse, the implementation _shall_ use reserved identifiers. – edmz Sep 27 '15 at 12:25
  • 8
    The standard library *is* part of the "implementation". It's defined in the C and C++ specifications just like the languages themselves. – Some programmer dude Sep 27 '15 at 12:25
  • This is clearly not a duplicate of http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier. It's about what the reason for the double undescores in the stdlib implementations is. `Though shalt` is not a good reason unless you're a sheep. Ensuring that no user defined macro with a legal identifier will ever clash with a stdlib header is. – Petr Skocik Sep 27 '15 at 13:12

1 Answers1

5

There is a good reason for the standard library to start internal names with two underscores: Such names are reserved for the implementation.

Imagine you write the following code:

#include <iostream>

using namespace std;

long square(long x)
{
  return x*x;
}

int main()
{
  cout << square(3) << endl;
}

I guess you would not be happy if this ended up calling some internal function square(int) used in implementing the standard library and doing something completely different, because it's a better match than your square(long) for square(3).

By prefixing all internal names with double underscores and at the same time the standard declaring that you are not allowed to do the same, the standard library authors ensure that something like this cannot happen.

Now you may say that <iostream> is not part of the STL, but every standard library header may include any other standard library header, so iostream may well include an STL header for use in its implementation.

Another reason why identifiers with double underscores make sense even in the case of local identifiers that are not seen externally is that you might have defined a macro of the same name. Consider:

#define value 15

#include <iostream>

int main()
{
  std::cout << value;
}

This is legal code and certainly should output 15. But now imagine what happened if some object in iostream declared a local variable names value. Your code obviously wouldn't compile.

Note that the standard library is part of the implementation (it's described in the C++ standard, after all), so it can use reserved names however it likes.

celtschk
  • 19,311
  • 3
  • 39
  • 64
  • Answers to bad questions are not useful. Please read George Cummins's post [here](https://meta.stackoverflow.com/questions/252506/question-quality-is-dropping-on-stack-overflow). – edmz Sep 27 '15 at 12:30
  • There's static functions (=anonymous namespace functions) to prevent clashes at the global scope. The std namespace takes care of that too. For struct/class members, there's private. Plenty of means outside of the double undescore in my opinion. – Petr Skocik Sep 27 '15 at 12:31
  • 3
    @black: This question clearly doesn't fall into the "mechanical turk" category. I also don't agree that it is a bad question. – celtschk Sep 27 '15 at 12:32
  • static/anonymous namespace functions are solutions to a *different* problem, namely clash of names *between* translation units (in particular, you often do explicitly *not* want them in the standard library as they would induce unnecessary bloat, not to mention that sometimes the identity of an entity is important). About the namespace, note that my example code explicitly used `using namespace std;` — surely bad practice, but definitely allowed. Putting them in a different namespace might cause issues with ADL. I've never seen double underscores used on class members. – celtschk Sep 27 '15 at 12:37
  • This http://stackoverflow.com/questions/21694302/what-are-the-mechanics-of-short-string-optimization-in-libc shows a piece of stdlib code where all member variables are prefixed with __. – Petr Skocik Sep 27 '15 at 12:41
  • 2
    @PSkocik: Thinking again about it, it makes actually sense: You could have defined a macro of that name, after all. I'll amend the answer. – celtschk Sep 27 '15 at 12:45
  • Notice that the OP's asking _why_ they are used, which is clearly a dupe, because they have to, choicelessly. Had he asked about _alternatives_, that would be a good one. – edmz Sep 27 '15 at 12:48
  • @celtschk Potential clashes with user-defined macros is a good reason. Will accept that answer. Thanks! – Petr Skocik Sep 27 '15 at 12:49
  • So there's no way to make the STL implementation less unreadable. Great. – Enlico Mar 24 '22 at 16:11