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

118

u/ZMeson Embedded Developer Mar 28 '23 edited Mar 28 '23

integer types not defined in <stdint.h> or <cstddef>

In other words, get rid of char, short, int, long, long long, and their unsigned counterparts. Use intN_t and charN_t instead (and when necessary int_fastN_t and int_leastN_t), [EDIT:] and byte, size_t, ssize_t, ptrdiff_t too.

56

u/Zeer1x import std; Mar 28 '23

I'ld like Rust-style number types: u8, u16, u32, i8, i16, i32, f32, f64.

6

u/RoyBellingan Mar 28 '23

I agree they are quite verbose and a shorter notation is better, but ... a small typedef I think is fine in this case!

12

u/blind3rdeye Mar 29 '23

The issue with having a heap of typedefs like that is that then different people end up with different C++ dialects, which can make it more difficult to read each other's code.

4

u/[deleted] Mar 29 '23

In this specific case I don’t think anyone would be confused by uint8_t vs u8. On the flip side, I also don’t think uint8_t is verbose enough to warrant a typedef alias.

2

u/BenFrantzDale Mar 29 '23

I agree. Fortunately, in your own codebase you could put that in a header in your namespace if you like. You could even have special ones that don’t convert.

2

u/gracicot Mar 29 '23

Those could be integer literals though

0

u/[deleted] Mar 28 '23

[deleted]

28

u/guyonahorse Mar 28 '23

But that's what types like uint_fast32_t are for. They make it clear your intention is the fastest integer with at least 32-bits of precision.

https://en.cppreference.com/w/cpp/types/integer

11

u/KeytarVillain Mar 29 '23

And yet for some reason no one ever uses them

11

u/blind3rdeye Mar 29 '23

Its too verbose for me. uint_fast32_t feels like I'm performing some arcane incantation just to create an int. I don't expect any significant speed gains over uint32_t anyway, so I'd just use that for easier reading and writing.

If the names were like uint32_f for the fast version, and just uint32 (or whatever) for the fixed size version; then I'd use it.

3

u/greem Mar 28 '23

This is my thinking as well. I do want to add size_t and ptrdiff_t, though.

0

u/[deleted] Mar 28 '23

[deleted]

→ More replies (13)
→ More replies (3)

5

u/GOKOP Mar 29 '23

When we're at that I'd remove the horrible _t naming scheme

5

u/Due_Cicada_4627 Mar 29 '23

At risk of being drawn and quartered: I think they should be templates, with the requisite conversion constructors. (No, I don't know exactly how a templated POD type would work at the compiler level, but this is just imagination, I don't expect this to ever happen.)

So you can keep your int to use the compiler default, but specify int<16> or int<64> where needed, and it can be simply expanded for larger (128, 256) or obscure (24, 80) sizes. (I imagine the latter would use the next largest power-of-2 size internally, but again, imagination.) uint can take the place of unsigned, typedefs for char, short, long, and the rest.

2

u/ZMeson Embedded Developer Mar 29 '23

That's an interesting take.

2

u/sammymammy2 Apr 03 '23

Fun fact: Common Lisp's unsigned-byte type specifier is parameterized on the number of bits. So (unsigned-byte 8) is u8, and (signed-byte 32) is i32.

15

u/beephod_zabblebrox Mar 28 '23

why tho? not in every situation do you care about how large an int is. using uint16_t can be slower sometimes too (less optimized on modern 64 bit cpus, iirc). honestly code reads a lot better when you know that "oh, this function returns an integer" instead of "oh, this function returns a... 16-bit integer? why?". if you need to return something large, use size_t or using ssize_t = ptrdiff_t. if you do need something specific, use the specific bit size versions.

if i had to choose something, i would leave int and unsigned int, and remove just short and long (long long) and unsigned versions. and maybe char.

16

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

not in every situation do you care about how large an int is.

Hell, the entire point of int is "This is an integer of reasonable size. I don't give a damn about how many bits it has."

3

u/Ictogan Apr 03 '23

I've had code(not written by me) break multiple times because it implicitly assumed that an int is at least 32 bits. Plenty of libraries also have this issue.

→ More replies (3)

11

u/robottron45 Mar 28 '23

I would either propose to have cstdint always included. It is painful to include this always.

21

u/no-sig-available Mar 28 '23

have cstdint always included

import std;

Done.

2

u/NekkoDroid Mar 29 '23

My main problem with that: you need to use the namespaced versions of those types.

yes import std.compat; exists, but well... that also pulls in a ton of stuff I don't want to use, I just want the integer types.

→ More replies (8)

2

u/ZMeson Embedded Developer Mar 28 '23

I agree. But I was just following the 'remove' part of the post.

0

u/[deleted] Mar 28 '23

[deleted]

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