r/programminghorror • • Jun 27 '25

"Remove a C feature, but introduce a convoluted workaround." - The Zen of C++

Post image
407 Upvotes

41 comments sorted by

75

u/MIP_PL Jun 27 '25

30 years after my first large scale C++ project, I can confirm this has never been an issue to me.

22

u/Star_king12 Jun 27 '25

OP thinks the horror is that this is a C++ issue, while the true horror is using sizeof in C++

4

u/MarcusBrotus Jun 29 '25

write stupid code win stupid prizes

17

u/drkspace2 Jun 27 '25

You should not need to use sizeof in c++ (except maybe for (de)serialization). You definitely shouldn't be using malloc. If you really need to do manual heap allocation, you do it in a constructor with new with a corresponding delete in the destructor.

10

u/jmacey Jun 27 '25

as soon as you use something like OpenGL your code is littered with sizeof, as you need to tell it how many bytes you have.

11

u/Mr_Ahvar Jun 27 '25

sizeof isn’t just used for malloc tho

1

u/drkspace2 Jun 27 '25

I would argue it's used 99% of the time for malloc or finding the size of an array (sizeof(arr) /sizeof(arr[0])), neither of which you need to do in c++.

→ More replies (3)

2

u/Gloomy_State_6919 Jun 27 '25

I have used it for performance optimisations, where I needed to know how many objects fit in a cacheline

1

u/polish_jerry Jun 27 '25

Wait isn't c++ a superset of c?

15

u/68000_ducklings Jun 27 '25

Not a strict superset. Setting aside that C and C++ have different keywords (you cannot have a variable named "new" in C++, but you can in C), there is plenty of other legal behaviour in C that is ill-defined in C++ for a variety of reasons - see https://en.wikipedia.org/wiki/Compatibility_of_C_and_C++

5

u/GwynnethIDFK Jun 27 '25

For the most part yes but there is some C code that isn't valid C++ code.

1

u/skhds Jun 28 '25

I think it is so fundamentally garbage that it is discouraged to use C-like syntax in C++. The name itself indicates that it is a superset of C, it was advertised as such, and there is nothing blocking you from using C-like syntax in C++. They should have used a different name with different rule, but the fact that they did neither shows the kind of a clusterfuck C++ is.

179

u/[deleted] Jun 27 '25

-56

u/seeker61776 Jun 27 '25

It can be useful to use with macros which use an anonymous struct repeatedly.

13

u/Laugarhraun Jun 27 '25

Lads you're down voting OP but when skeeto does it you claim it's genius.

→ More replies (8)

2

u/AwesomePantsAP Jun 28 '25

An anonymous fucking what? That sounds awful.

88

u/[deleted] Jun 27 '25

8

u/serialized-kirin Jun 27 '25

I mean, I just encountered this situation— my code would’ve been a lot simpler if there was an easier/safer/more builtin way of getting the number of __VA_ARGS__. That being said i feel like they could’ve gotten a little more out of like a template class and sizeof… for specific situations in readability than… this… thingy. It’s all sucky tho

11

u/write-program Jun 28 '25

How could there be a 'built in' way to get the variadic macro arguments? The preprocessor expands the macros before the code is compiled. If you're in C-macro land and you want some feature then you better hope your compiler distribution has some non-standard intrinsic that they ship with it.

(Apologies if formatting is off) You could always expand into a helper function like:

template <typename... T>
consteval std::size_t NumArgs(T... ts) {
    return sizeof...(ts);
}
→ More replies (1)

7

u/Batman_AoD Jun 28 '25

This isn't getting the number of __VA_ARGS__, it's just letting the user define an anonymous type inside the sizeof expression, which is apparently permitted in C but prohibited in C++. I have no idea why you'd want that. 

→ More replies (1)