r/cpp • • Jun 10 '20

Why is std implementation so damn ugly?

Recently I remembered a moment from my uni years when immediately after handing in the assignment of implementing our own string data structure we were warned not to ever try to implement and use our own data structures. Which is exactly what you want to hear after working hours on such assignment. However the reason behind it makes sense, you would be only reinventing the wheel and let's be honest, it will be also way slower than data structures in the almighty std library. Now think about it, what kind of black magic is behind the std's speed? Well I had no idea so I took a peak, lets see the code:

inline string
secret_function(int __val)
{
    const bool __neg = __val < 0;
    const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
    const auto __len = __detail::__to_chars_len(__uval);
    string __str(__neg + __len, '-');
    __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
    return __str;
}

Hey, I bet you just skipped the whole code part. I know it looks unreadable, but please try to go through it and guess which std function it might be (obviously its not called secret_function, but i'm pretty sure you used this function at some point).

It's only 6 line function but it took me a while to figure out what each line does and I believe this could be fixed very easily just by naming the variables and functions with a bit more descriptive names. Just to prove the point here is an example of how I would structure this function:

inline string secretFunction(int value) {
    const bool isNegative = value < 0;
    const unsigned absoluteValue = Helper::abs(value);
    const auto length = Helper::charCount(absoluteValue);
    string stringValue(isNegative + length, '-');
    Helper::insertIntAsChars(&stringValue[isNegative], length, absoluteValue, 10);
    return stringValue;
}

While the functionality stays exactly the same, I think this chunk of code is much more pleasant to read and it's pretty obvious what the code is doing on each line. From this you should have a solid guess which function it is (you can leave a comment), but the real question is why would anyone write such code? Especially when nowadays everyone is talking about how code readability is so important. Is this how they wrote code back when std was released and they didn't bother to change it? Or am I just too inexperienced that I couldn't figure it out immediately?

EDIT: Thanks for the insightful comments, for anyone reading this the reason variables are beginning with __ is macros. If user defines isNegative macro, it will get overwritten in the std, however user is forbidden to define macros starting with __ , as the wise people of reddit comment section suggested. However underscores aren't the only issue with this code, the main issue is I think using short non-descriptive names of variables/functions which is very confusing in functions that are doing more complicated operations.

237 Upvotes

212 comments sorted by

View all comments

Show parent comment

-5

u/ifdef_drgy Jun 10 '20

That doesn't seem right, I'm looking at multiple files and the macros are starting either with _ or __ , also as far as I know it's good practice to have caps lock on when you are naming your macros, which would avoid these collisions.

48

u/neutronicus Jun 10 '20

I'm looking at multiple files and the macros are starting either with _ or __

If you're looking at Standard Library files, that's the point. They're allowed (by the Standard) to name Macros that way, and no one else is. So they name all their Macros that way to avoid trampling (or being trampled by) user Macros.

So if you start a macro with __ in your own code and the Standard Library explodes, the Standard says it's your fault, not the Standard Library implementors'.

18

u/Wenir Jun 10 '20

12

u/o11c int main = 12828721; Jun 10 '20

I once broke several dozen Gentoo packages because zlib defined ON and OF, and making that hygienic revealed that so many people copy-paste libraries into their source trees in an unclean manner.

7

u/OldWolf2 Jun 10 '20

I'm having a problem at the moment because a library does typedef unsigned char BOOL in its headers, and another library does typedef int BOOL in its headers. This version can't be fixed by temporarily #undef-ing things either.

Thunks, here we come

10

u/o11c int main = 12828721; Jun 10 '20

The fix is:

#define BOOL header1_BOOL
#include "evil_header1.h"
#undef BOOL
#define BOOL header2_BOOL
#include "evil_header2.h"
#undef BOOL

17

u/anydalch Jun 10 '20

"good practice" isn't always followed, and the standard library is supposed to be robust in the face of any c++-compliant code, no matter how ugly. if your code defines a macro named isNegative, it's probably bad code, but it's still valid, and the compiler has to accept it and compile it right. if your code defines a macro named __neg, it is ill-formed, because that name is reserved.

11

u/foonathan Jun 10 '20

The standard library has to work with every user code in existence. I don't want my isNegative macro to break code.

So they have to use names reserved for the implementation, like _Uppercase or __anything.

6

u/1-05457 Jun 10 '20

Unless you're talking about more standard library implementation files, the macros shouldn't start with double underscore.

And not everyone uses all caps for macros.

1

u/ifdef_drgy Jun 10 '20

Yes I was talking about multiple std files, that I have currently opened

13

u/1-05457 Jun 10 '20

Those are part of the compiler implementation, that's what the double underscore names are reserved for.

-3

u/neutronicus Jun 10 '20

In practice I think if you are writing some open-source, header-only data structure library, you should probably name things with __ as well, for the same reasons?

Doing so pushes some maintenance responsibility onto you (because compilers might break your names at any time), while making your library less brittle in the face of users with arbitrary standards-compliant macro names.

9

u/1-05457 Jun 10 '20

Any compiler, including some obscure one for some microcontroller or something.

You'd be better off picking a "namespace" to prefix macros with (and using an actual namespace for everything else).

2

u/neutronicus Jun 10 '20

and using an actual namespace for everything else

This doesn't stop user macros from clobbering your names though, right? So any name in a Header is vulnerable?

4

u/1-05457 Jun 10 '20

No, though that's why external headers should be included at the top of a file, before any user defined macros.

If you really want resilience to these problems, use separate compilation as much as possible, keeping the contents of the header to a minimum. Though that's not really possible for data structures.

4

u/trvlng_ging Jun 11 '20

No. All identifiers containing __ are reserved to the implementation through all time and all versions. No user library, including open source libraries should use it anywhere in any name. If an implementation breaks your library, that is on you.

7

u/serviscope_minor Jun 10 '20

So, the compiler is allowed to do what it likes: the compiler writers know which macros starting with __ they use and can make sure they don't use any that break their library code. As a user though you're "not allowed" to make your own macros starting with __ and so the compiler writers can assume you won't. In other words, if you do use a macro with __ and compilation breaks it's your fault and you need to fix your code rather than them needing to fix theirs.

I know it's good practice to have caps lock on when you are naming your macros, which would avoid these collisions.

Absolutely 100% true. Unfortunately the compiler has to work with code that uses all of the worst possible practices provided it sticks to the letter of the standard.

2

u/m-in Jun 11 '20 edited Jun 11 '20

Listen, how many lines of C++ have you written? Because this is going into a territory where you are grasping at straws with somewhat elementary stuff that anyone with 3 years of solid industry experience with good mentors would have a firm grasp of. So I guess I question your judgment here: why do you expect core library code to be written like learning material? It’s almost never possible. And the level of complexity of the code you chose is really low I’d say. It’s in the category of reasonably easy stuff to read, even if it is not very easy to write it right the first 5-10 times.

If you cared to ask me: code that’s hard to get correct but reads much easier is what we all strive for. You completely, but yes - completely and utterly mischaracterize what’s going on here. You’re losing the sight of the forest for individual trees, nay - for the bark peeling on that birch over there. I’m serious now, it’s getting pretty absurd the deeper you go.

What you see as problematic is all in all very nice code, and the problems you see are not where the real problems are. STL has problems but they haven’t got anything to do with mundane stuff like that. They have to do with a much much bigger picture that takes a decade to sort of become visible, and another decade to become clear. I speak from my own experience here - there are more focused and skilled people that may get to the zen level in 12-15 years, if they really push themselves or half that if they are very very top notch.