Settings

Theme

Defective C++

yosefk.com

17 points by _chu1 2 years ago · 9 comments

Reader

markisus 2 years ago

It’s funny because you really have to have a lot of experience with the language to articulate these issues. C++ is the worst language, besides all the other ones, when it comes down to performance and third party library availability.

I’ll just throw in my own pet peeves here.

The existence of .h files is still a big problem. I’m not aware of another language (besides c of course) that basically forces you to type the same letters twice in slightly different ways in order to expose a public api.

I also don’t understand why there’s still no way to print an enum value.

However there is some progress. I think std::span fixes the issue with the vector of const vs const vector. The Eigen library gives a (not perfect, but very convenient) matrix type. And I think I heard something about modules?

  • pajko 2 years ago
  • foofie 2 years ago

    > The existence of .h files

    C or C++ don't specify or require .h files. The languages don't even specify any file extension.

    What C and C++ specify is the concepts of declaration and definition, and require only one definition across all translation units. Thus it's customary to simply have a single source for declarations to ensure they all stay consistent in spite of their usages, and those declarations are used once by wrapping them in include guards.

Bostonian 2 years ago

Should have (2009) in the title. I don't know enough about C++ to say which of the problems have been fixed since then.

thegrim000 2 years ago

Well the very first item in the list is wrong so I stopped reading there. Have your interface between systems be pure abstract virtual classes so that changing private members in a concrete subclass doesn't propagate the change to the consumers.

  • guenthert 2 years ago

    To expand on this, "In naturally written C++ code, changing the private members of a class requires recompilation of the code using the class." -- uh, the Pimpl pattern has been idiomatic C++ for a long, long time (http://www.gotw.ca/gotw/024.htm).

    • taejo 2 years ago

      I assume that's exactly the unnatural way that's being referred to. The natural way to define a member variable is

      Complextype m;

      which has the fewest tokens, is the easiest to read, and doesn't require extra memory/lifetime management or library types that didn't even exist in the standard library for the first decades of the language (i.e. std::unique_ptr) so certainly weren't the originally intended way to declare members but are workarounds for a problem in the original language.

      Of course, the solution to this (and some of the other problems discussed here) in other languages is often make everything a heap pointer under the hood, and there are good reasons not to do that!

      • foofie 2 years ago

        > The natural way to define a member variable is (...)

        No, that's not right at all. Your example is only a declaration of an incomplete type. You cannot do anything with it except.... Define a pointer to the incomplete type, and use that to keep the type definition hidden.

        That technique is called pointer to implementation - pimpl. The thing you're complaining about?

        • taejo 2 years ago

          OK, set me right. How do you declare a member variable "m" of type "Complextype" in C++?

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection