News for hackers. Stuff that matters.
Re:C++ Frequently Questioned Answers (2009) by einpoklum on Sunday January 23, 2022 @09:53AM (#30046531)
C++ certainly has flaws - but it also has intentional design choices, which are important for achieving its goals. Also, as others have mentioned - much has improved since 2009.

To make this concrete, Let's go over the "C++ defects" summary appearing at the link, before the individual "frequently-questioned answers".

> changing the private members of a class requires recompilation of the code using the class. When the class is used to instantiate member objects of other classes, the rule is of course applied recursively.

This is mandated by two of C++' language design goals:

1. Performance of compiled C++ code: This recompilation is necessary for exploiting optimization opportunities involved the altered private members. In fact, these days, compilers goes beyond even that situation: _linking_ code may trigger partial re-compilation without any changes to the source.

2. Zero-overhead abstractions: Having a private member should not in itself cost you in terms of performance.

However, we can make the class implementation opaque (e.g. using the PIMPL idiom: https://stackoverflow.com/tags/pimpl-idiom/info ) if we want/need to.

> Outstandingly complicated grammar

C++ is guilty as charged here! It even has a semi-idiom named the "most vexing parse": https://en.wikipedia.org/wiki/Most_vexing_parse

But there is an excuse, which is: Backwards compatibility. First, with C and C's syntax; then, with earlier versions of the language. Fixing the complicated syntax would break all existing C++ versions. So, they don't do it.

> No way to locate definitions

The fix for this is now part of the language: Modules. Unfortunately it will take time before these are widely-enough adopted.

> No run time encapsulation... C and C++ are not designed to run in managed environments

This is again necessitated by the performance requirements. Run-time encapsulation would mean slower code, sometimes much slower.

Also, such encapsulation would mean there would be room for another C++-like language, below C++, without this encapsulation... and a goal of C++ is not to leave such room.

Luckily, in recent years we have undefined behavior sanitizers in compilers, which catch a lot of these cases.

One should also remember that languages with managed runtime environments often have exceptions to the rule, i.e. unsafe/unmanaged sections (unsafe in Rust, JNI in Java).

> No binary implementation rules

Sorry to have to harp on this, but platform-specific ABI is another necessity of performance. Still, C++ has a long way to improve in this respect, e.g. https://stackoverflow.com/q/58339165/1593077

Recently, stack traces have been introduced into the language, and in C++23 they might be available by default when throwing exceptions; see: https://wg21.link/p2370r2

(I may continue in another post or extend this one.)

Re:C++ Frequently Questioned Answers (2009) by colonwqbang on Sunday January 23, 2022 @12:51PM (#30048396)
Pimpl seems to me like a workaround for deficiencies in the language. It's verbose and inconvenient. We should be able to add private member functions to a class without having to recompile all files that use the class. We should be able to forward-declare a class including public member functions without also declaring the member variables of the class.

Even in C I can say:

struct A;

void public_member_function(A *a);

I can also add as many static functions as I like later without affecting the public declararation of struct A.

Why not in C++?

Re:C++ Frequently Questioned Answers (2009) by ncmncm on Sunday January 23, 2022 @06:45PM (#30051915)
You may do that in C++, too. It would look, literally, exactly the same. It would compile, and run, identically. And, it would be exactly as error-prone as the same code compiled with a C compiler.

Extra support in the language for automating PImpl types could be welcome, but might not offer enough benefit to offset the complexity.

Re:C++ Frequently Questioned Answers (2009) by colonwqbang on Monday January 24, 2022 @07:36AM (#30056871)
Why do you feel that it's more error-prone?

function(a);

vs.

a->function();

I can accept that some think the second version looks nicer. But I don't buy that we should have to jump through hoops like pimpl just to get there for non virtual calls.

Re:C++ Frequently Questioned Answers (2009) by ncmncm on Monday January 24, 2022 @06:39PM (#30065746)
Nobody cares how it looks.

What matters is that your "a" is a naked pointer. Pointers have reference semantics, not value semantics, so are inherently error-prone. A pointer might be null. A pointer might be already freed. A pointer might be left over from some other use.

A PImpl object is a value, with value semantics. It owns the wrapped pointer, and whatever that points at. When it goes out of scope, it cleans up. If you throw, it cleans up. When it is copied, the right thing happens, if that is allowed, or cannot happen, if not. When it is passed to a function, the right thing happens. When it is returned from a function, the right thing happens. The "right thing", in each case, is visible in exactly one place in the source code, and nowhere else. Nothing using it can do any of it wrong.

This is all really fundamental stuff.

Re:C++ Frequently Questioned Answers (2009) by zozbot234 on Sunday January 23, 2022 @01:54PM (#30049113)
PIMPL is not a workaround for language-level deficiencies, it's a workaround/pattern for ensuring a forward-compatible ABI.
Re:C++ Frequently Questioned Answers (2009) by einpoklum on Sunday January 23, 2022 @03:58PM (#30050375)
(continued from parent post)

----

> No reflection ... impossible to ... iterate over the methods or the attributes or the base classes of a class ... [or] programmatically determine the type of an object...

There's run-time reflection, and compile-time reflection. C++ has almost no runtime reflection - and this is again due to performance considerations. That would have a lot of overhead.

As for compile-time reflection, C++ has gained some over the past decade. decltype() gives you the type of an object. A static reflection Technical Specification is out, see: https://en.cppreference.com/w/cpp/experimental/reflect . It will allow much of what the author was decrying. It may make it into C++23, and some compilers already partially-support it. Now Is it very late in arriving? I suppose it's fair to say that. But TBH, it's quite difficult to design this for a 40-year-old language with a lot of baggage and not within a managed runtime. And achieve near-consensus about it.

----

> Very complicated type system

The type _system_ of C++ is not very complicated. What the author seems to dislike is how common standard-library types are actually somewhat complex constructs within the type system rather than atomic types or simple constructs. A language in which "string" is an atomic type might have std::map<string, string> rather than the complex-sounding `map<std::basic_string<char, std::char_traits<char>, std::allocator<char>>`, `std::basic_string<char, std::char_traits<char>, std::allocator<char>> >`. Still, that longer type is simply `std::map<std::string, std::string>`, and if you `use std::string;` then it's `std::map<string, string>`. (Yes, there are other template parameters to map, I know, I'm making a point).

In fact, one could argue that it is a virtue of C++ of having a non-complex type system: The complex types are actually implemented _in C++_, with no need for a rich gallery of atomic types. Other systems put things like lists, associative arrays, optionals (a.k.a. maybe's) etc. out of the reach of "mere mortals".

Then again, the choice of primitive types or type constructs in C++ is not great due to inheriting from C. C++ can construct new types as arrays-of, structs-of, unions-of, references-to, pointers-to and bit-fields-of. arrays are annoying with their decay behavior and unassignability, and non-discriminated unions are also useful mostly for low-level works. It would perhaps have been nicer if arrays behaved like std::arrays and unions like std::variants, and for the C-style types we would have used reinterpret-casts and other dirty tricks. But then, what would you want to happen when a constituent element of a variant throws an exception during construction? Is the variant in a valid state after that exception is caught? The answer is not at all trivial, so richer atomic types require making choices that aren't always right for everyone.

> if your function accepts const std::vector<const char*>& ... and I have a std::vector<char*> object ...then I can't pass it to your function

Well, not exactly. I mean, you actually can pass it to the function, if you insist: `my_func(reinterpret_cast<std::vector<const char*>&>(my_vec)` ... but that's not very nice. So, yeah, this is definitely an issue.

However:

1. There is no inherent reason that type T<E*> should be usable as a T<const E*>. Those could be completely different types For T = std::vector the types just happen to correspond nicely. I doubt that other languages address this, though.

2. If you want to be a wise guy, and tell me "other language would just have a string and a const string instead",, then - you wouldn't have this problem, since there is no such thing as an std::vector<const E>; that doesn't compile.

3. If your function takes `std::span<const some_string_class>`, you again don't have this problem.

so, a bunch of mitigating arguments + you can still do it if you really want to.

----

> Very complicated type-based binding rules

They're only complicated if you make them complicated. If you single f(), there won't be any complex overload resolution; and if you write a bunch of f()'s, there will.

As for the standard library, it's overload resolution can get complicated; but error messages have improved significantly! First, we got static_assert()'s in C++11 which explain what's the problem with (hopefully) simple text. Then in C++17 we got some deduction guides, so you fail less often. Then in C++20 we got concepts - which themselves are quite complex, but you don't have to worry about them; the point is that your error messages for overload resolution improve again. Don't get me wrong: There will still be long errors about failure to resolve, but they're more readable these days.

----

> Defective operator overloading

The only argument I saw here was:

> overloaded operators have to return their results by value - naively returning references to objects allocated with new would cause temporary objects to "leak" ...

1. In C++, we pass things by value unless there's a good reason not to.

2. If you're worried about copying when returning - that doesn't happen. This used to be a compiler optimization, but since C++17 the language guarantees it: https://en.cppreference.com/w/cpp/language/copy_elision . The return type doesn't even have to be copyable or movable!

3. Don't use new, it's rude these days: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines... ; if for some reason you must allocate dynamic memory, use std::unique_ptr, or std::shared_ptr, or return an allocating container class instance, by value.

---

> Defective exceptions

C++ exceptions could of course be improved in various ways (e.g. they may soon be getting stack traces); but not along the author's line of argumentation.

> If we use exceptions, we have to write exception-safe code - code which frees all resources when the control is transferred from the point of failure

You need to write "exception-safe" code anyway! Acquire resources on construction, release them on destruction. Also known as CADR or RAII.

> ... and the vast majority of "resources" happens to be memory, which is managed manually in C++

Effectively - no, it isn't. And that's exactly because we're writing that "exception-safe" code: Since we _don't_ just write new nor delete (see above)

> To solve this, you are supposed to use RAII ... meaning that all pointers have to be "smart"

1. No they don't - only the _owning_ pointers need to be smart (or in container classes etc.); and the non-owning pointers you don't have to free.

2. What's your obsession with pointers, anyway? Pointers in C++ are so last-millenium...

----

> Duplicate facilities

Yeah, C++ has some of those. I mentioned std::array vs C arrays and std::variant vs C unions. That's the backwards compatibility for you... but also, don't mistake _similar_ facilities for _duplicate_ ones, like the author does:

> If you need an array in C++, you can use a C-like T arr[] or a C++ std::vector<T>

Those are different! `T arr[N]` has fixed size determined at compile time, and its elements typically resides on the stack; `std::vector<T>` has variable size and its elements typically reside on the heap (allocated with new). In fact, C++'s standard library is _missing_ many important array-like classes! See: https://stackoverflow.com/a/67409339/1593077

> That's because C++ doesn't have garbage collection, since that, folks, is inefficient.

It's not just inefficient; by now, it's effectively unnecessary. See: https://stackoverflow.com/a/48046118/1593077