0

The more I learn about C++ the less sense it makes to me that the C++ standard library source code (where all those std functions are implemented) makes heavy use of variable names with __ prefix. Like std::addressof() uses __r as its single argument. I tried to get meaningful answers why this is the case (even asked ChatGPT!) but no luck. I know that __ prefix is reserved and user code cannot use such names. But how could user code collide with, say, the argument name of a function in std?! That's what's puzzling to me. Same goes for private members in std. Even local variables start with the __ prefix! What's the point of using __ prefix in those cases? It's almost like the standard library implementers are suggesting through their practice that somehow hiding implementation details from user code depends on this convention. That, without this practice, users could somehow rely on those names that 'were not intended for them'.

By the way, this makes C++ standard library source code much harder to read. Maybe intentionally so?

Lajos Nagy
  • 9,075
  • 11
  • 44
  • 55
  • 3
    "But how could user code collide with, say, the argument name of a function in std?!" Macros. – Retired Ninja Jun 27 '23 at 09:58
  • "Macros." ... how? Could you provide an example? – Lajos Nagy Jun 27 '23 at 10:03
  • 3
    For fun `#include ` and try to use `std::min` and `std::max` [Why is std::min failing when windows.h is included?](https://stackoverflow.com/questions/5004858/why-is-stdmin-failing-when-windows-h-is-included) – Retired Ninja Jun 27 '23 at 10:03

1 Answers1

1

Suppose they would use "normal" identifiers. Suppose the implementation uses foo as an identifier. Then you might write this code:

 #define foo 
 #include <some standard header>

If some standard header uses the identifier foo bad things might happen.

Thats why for the implementation there are certain identifiers resevered. You must not use them because otherwise there might be conflict. The implementation must use them, otherwise there might be conflict as above.

See here for details: https://en.cppreference.com/w/cpp/language/identifiers

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185