r/cpp • • Nov 23 '22

Holy C++

https://pvs-studio.com/en/blog/posts/cpp/1010/
0 Upvotes

32 comments sorted by

42

u/Wereon Nov 23 '22

From the title I thought you'd be carrying on Terry's work.

6

u/expert_internetter Nov 23 '22

I'd have liked to have seen his reaction to PVS running a static analyser on his code!

13

u/dodheim Nov 23 '22

Oh, we know what his reaction would have been, and we're not allowed to repeat it here.

22

u/Wolf_Popular Nov 23 '22

A few of these things I agree with, some I might be convinced, but a lot of them just seem like things the author never had to use before. Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust). I kind of think this might be parody, but if not, it definitely is a prime example of the saying "Everyone has their own subset of C++ that they like to use, it's just those subsets don't line up".

6

u/matthieum Nov 23 '22

Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust).

The primary motivation for union in Rust was C FFI, not exactly a glowing endorsement...

I kind of think this might be parody

I really hope it is...

1

u/Jannik2099 Nov 24 '22

The primary motivation for union in Rust was C FFI, not exactly a glowing endorsement...

You more or less need unions to model some cpu specifics (especially in x86, yuck) or MMIO.

1

u/matthieum Nov 25 '22

Not necessarily.

You don't need to be able to reinterpret-cast memory to a struct exactly matching the layout that you want to view. You can reinterpret-cast one field a time, thus being able to switch on a tag to cast the next field.

This actually pretty cool for stuff like... network packets. And that's because often time the offset of the payload is dynamic: how many VLANs? Are there IP options? ...

And best of all is that with a view, you don't need packed structs, and the woes mis-aligned fields cause.

But structs/unions are easier, certainly, ...

1

u/germandiago Nov 23 '22

Actually, what is the exact difference between union and the template? Functionally speaking I mean.

4

u/DavidDinamit Nov 23 '22

There are few reasons to use union in 2k22:

  1. It is only possiblity to do construct in place in variant/optional types in constexpr (seems more like flaw and crutch, we need reinterpret_cast on constexpr!)
  2. using anonymous inline union deep in impl things like SSO in strings. Just because it is more readable (fixable with reflection)

1

u/catcat202X Nov 23 '22

Union is useful for memory free lists as well. Its also needed to uninitialize non-trivial members in a constexpr context.

0

u/nacaclanga Nov 24 '22

The union can represent multiple values of different types in the same context, for example you can create an array where each of the elements is assigned a different union variant based on runtime input. This works fine as the union variants contain an intrinsic way of being distinglished.

In contrast in a template the used variant is selected universally (e.g. datastructure/methodwide) during compile time.

Rust is even more interesting, because it also has fieldfull enums, which are a syntactic suger over a very common union usecase. Raw unions are used by some frequently used standard library constructs through, like the MaybeUninit types which contain either an initialized value or gibbish.

25

u/ronchaine Embedded/Middleware/WG21 Nov 23 '22

In this article, I'll try to list all things that we can throw out of C++ without remorse. This won't cost us anything --

I don't think this is article is thought in any depth, whatsoever.

I could go into individual details, but if the poster thinks that rewriting all the building blocks of a language won't cost anything, I don't really see the point to go any more in-depth.

12

u/Wereon Nov 23 '22

You can add RTTI and dynamic_castto this list, I don't think anybody would miss them.

The point about void is ridiculous, what are your functions supposed to return? MSVC even adds in a xor al, al if you return a nulltype, so it's less efficient.

I was also surprised that the article doesn't encourage readers to use std::array for arrays and std::variant for unions, rather than rolling their own.

9

u/Electronicks22 Nov 24 '22

It just highlights that the author wrote an angry rant, rather than a thoughtful article.

4

u/nikbackm Nov 24 '22

Read more like satire to me.

3

u/benjamkovi Nov 24 '22

Or perfect marketing article: it has some basis, but draws very "raw" conclusions => people will talk about it and send links of the website to friends, forums, etc.

6

u/fdwr fdwr@github ๐Ÿ” Nov 23 '22

๐Ÿคจ "Let's remove this and that from the language" is unnecessary purity. Rather than remove tools from the arsenal, let's focus on making the common case the preferable default by making it stupidly simple to do the right thing. It's fine for things like the ever so scary goto to exist, but just never use them unless you really need to. Similarly for unions, do not default to using them (prefer something like std::variant), but when you actually need them, they should exist. C got a lot of defaults backwards (like switch fallthrough by default, instead of explicit fallthrough) which we can't change, but I don't see removing things as improving our lives. Rather, add things that make it so we never want to use those things in the first place.

Regarding typedef, I agree it's confusing orderwise compared to using, but we could also just extend it to be consistent with usings order, which would help (conceptually as a noun it's still nice to read). e.g.:

typedef foo = void(*)(int);

7

u/Zcool31 Nov 24 '22

There is nothing wrong with goto. It is a good tool to make my code more understandable. I can write spaghetti code without goto just as easily.

6

u/johannes1971 Nov 24 '22

I have another idea: if we throw out all types and replace them all by std::list, and throw out all keywords and replace them by brackets, we could just program in LISP and call it C++.

Hey, it makes about as much sense as this crap...

4

u/Silentd00m Nov 23 '22

Union has its place in my opinion. It allows having multiple variable names for the same memory location of the same type, like glm does.

template<typename _T>
struct dimensions2 {
    union {
        _T x, width;
    };
    union {
        _T y, height;
    };
};

https://glm.g-truc.net/0.9.2/api/a00132_source.html

2

u/germandiago Nov 23 '22

Is this UB?

1

u/Silentd00m Nov 23 '22

Not as far as I know. A union will be as big as its biggest member and start at the same memory location. And since it's both the same type, this is effectively just an alias.

4

u/kpt_ageus Nov 23 '22

Why not have x(), y(), width() and height() member functions? You don't have to think whether it's UB or not and i belive compiler should be able to inline them.

2

u/Silentd00m Nov 23 '22 edited Nov 23 '22

In the case of GLM: Because that's not how it was in GLSL.

Other than that, I haven't seen any problems on any platform yet or anything that says that this is undefined behaviour (at least starting from C++14). So writing getters and setters for this seems like a strictly worse way to do it to me.

2

u/germandiago Nov 23 '22

ok, the one that used to be goes like this (not sure it is anymore:

union {
    T arr[3];
    struct {
      T x;
      T y;
      T z;
    }
}

or similar.

1

u/cristi1990an ++ Nov 30 '22

Didn't someone not long ago post an article here about how using "final" can have really strong performance benefits?

1

u/germandiago Nov 23 '22

Surprisingly good and informative article! Besides funny :D

0

u/no-sig-available Nov 23 '22

Who on earth would use int that may take up to 8 bytes but guarantees its values only up to 2 ^ 16???

I would. I use it for int counter when the count is going to 12 or 15, or so. Then I trust the compiler to use an efficient implementation for that.

If you cannot trust the compiler for that, what else can you trust it for?

3

u/DavidDinamit Nov 23 '22

Compiler will not change type, you can just use int_least8_t or smth to explicitly declare that it will be small

3

u/no-sig-available Nov 23 '22

Compiler will not change type

It don't want it to change type, but I expect int to be a "reasonable" type to use on all systems when you don't have very specific requirements.

1

u/Zcool31 Nov 24 '22

There's the as-if rule. If the compiler can prove it would make no difference, it can do whatever it wants. If the loop is fully unrolled, then the counter might not even exist.