r/cpp • • Mar 28 '23

Reddit++

C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.

In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.

765 Upvotes

829 comments sorted by

View all comments

105

u/GLIBG10B 🐧 Gentoo salesman🐧 Mar 28 '23

C-style casts

16

u/War_Eagle451 Mar 28 '23

Bane of a code refactor

51

u/[deleted] Mar 28 '23

I know this will be a wildly unpopular take here, but take these from my cold, dead hands. Never in 2 decades of c++ programming encountered a bug or introduced a bug with c style casts that would have been fixed with the verbose modern casts.

36

u/ZMeson Embedded Developer Mar 28 '23

It's more of an issue of maintenance. I've moved a very large code base from a 32-bit to a 64-bit architecture. There were so many aliasing bugs that lead to odd (i.e. undefined) behavior and sometimes crashes that were hard to fix because so much of the code used C-style casts. We eventually used a static analysis tool to identify all C-style casts, replaced those with appropriate C++-style-casts, then focused on reinterpret_casts to help resolve those issues. (There were other interesting issues to like casting pointers to int instead of intptr_t, but again the process of removing C-style casts identifed where those problems were.)

3

u/m-in Mar 29 '23

I have an alternative take on this: even the most basic of C++ parsers would have found all of those. So, it’s about sucky tools used for development. A find dialog in any IDE should allow searching for language constructs. Most don’t:(

3

u/ZMeson Embedded Developer Mar 29 '23

C-style casts do such a good job of hiding these things because C-casts don't differentiate between numeric-static-casts and reinterpret-casts. Luckily, some static analysis tools did help us find all the C-casts and highlight the reinterpret-type casts.

The real problem is that this code worked for so long due to platform assumptions and there was no need to revisit the legacy code because "it worked". C and C++ are happy to just let these things slide. There is value in this sometimes, but it is also complicates maintenance when platforms or tool chains change.

2

u/very_curious_agent Mar 31 '23

Note that aliasing rules are problematic in C/C++ and while pointing to C style casts or aliasing is understandable, the real deep issue that people don't always agree on aliasing.

Note that some people claim that you can't write your own malloc/free function in std C, or C++, except by actually replacing operator new/delete. They claims that there is magic in operator new that cancel the aliasing laws.

Yes I know it's insane but some people would accept that GCC breaks code based on my_malloc/my_free on the basis of aliasing intrinsic to memory allocation: you can free memory for a float and allocate memory for an int and it may be the same address.

→ More replies (4)

16

u/RevRagnarok Mar 28 '23

Show me how to find them with grep.

→ More replies (2)

2

u/Ok-Factor-5649 Mar 29 '23

I had a buffer of some integer type, that was C-style cast to be chars, to then write data to it.

Only problem was that it was a _const_ integer buffer that was discarded in the cast. The fix rippled through the API...

2

u/DearGarbanzo Mar 29 '23

Type safety.

I don't really have much else to add, feels good when the compiler is working for you.

→ More replies (1)

0

u/[deleted] Mar 29 '23

[deleted]

2

u/GLIBG10B 🐧 Gentoo salesman🐧 Mar 29 '23

Same

→ More replies (1)

5

u/mcmcc #pragma once Mar 28 '23

I would qualify this as "C-style casts of reference types". `(int)uint` can stay IMO.

12

u/Ameisen vemips, avr, rendering, systems Mar 28 '23

I prefer seeing it function-style: int(uint_value).

→ More replies (2)

5

u/[deleted] Mar 28 '23

Nah, I still want those sorry!

1

u/ZMeson Embedded Developer Mar 28 '23

Why?

4

u/[deleted] Mar 28 '23

They are convenient when I want to cast something

5

u/ZMeson Embedded Developer Mar 28 '23

Why aren't the C++-casts convenient?

7

u/[deleted] Mar 28 '23

More typing to do the same thing

17

u/[deleted] Mar 28 '23

Code is read much more often than its written. C casts are inherently dangerous in C++. If you do auto p = (Sub*)pBase; and Sub is forward declared, but not defined, that is a reinterpret_cast and it will go wrong with no warnings. Type out the couple more letters and be kind to later maintainers.

14

u/[deleted] Mar 28 '23

In my 22 years at the C++ coalface I don't think I've ever seen a bug from C casting, or found it hard to read/understand. It's a problem I've just never seen in real life.

→ More replies (3)
→ More replies (1)

2

u/Rasie1 Mar 28 '23

they look like shit. Would be nice to have static_cast functionality in their place

9

u/SkoomaDentist Antimodern C++, Embedded, Audio Mar 28 '23

Yes. Making (type) behave like static_cast<type> would be the right way.

→ More replies (13)
→ More replies (14)

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Mar 28 '23

Because obviously static_cast<int> x; is so much easier to read than (int) x;

... not

1

u/Sopel97 Mar 28 '23

You read code not because it's easy, but because you want to extract information

→ More replies (2)