←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 3 comments | | HN request time: 0s | source
Show context
butterisgood ◴[] No.45268696[source]
A pet peeve of mine is when people claim C++ is a superset of C. It really isn't. There's a lot of little nuanced differences that can bite you.

Ignore the fact that having more keywords in C++ precludes the legality of some C code being C++. (`int class;`)

void * implicit casting in C just works, but in C++ it must be an explicit cast (which is kind of funny considering all the confusing implicit behavior in C++).

C++20 does have C11's designated initialization now, which helps in some cases, but that was a pain for a long time.

enums and conversion between integers is very strict in C++.

`char * message = "Hello"` is valid C but not C++ (since you cannot mutate the pointed to string, it must be `const` in C++)

C99 introduced variadic macros that didn't become standard C++ until 2011.

C doesn't allow for empty structs. You can do it in C++, but sizeof(EmptyStruct) is 1. And if C lets you get away with it in some compilers, I'll bet it's 0.

Anyway, all of these things and likely more can ruin your party if you think you're going to compile C code with a C++ compiler.

Also don't forget if you want code to be C callable in C++ you have to use `extern "C"` wrappers.

replies(6): >>45269029 #>>45270860 #>>45273486 #>>45277585 #>>45279380 #>>45280485 #
aw1621107 ◴[] No.45270860[source]
> C++20 does have C11's designated initialization now, which helps in some cases, but that was a pain for a long time.

C++ designated initializers are slightly different in that the initialization order must match the declared member order. That is not required in C.

replies(1): >>45271152 #
o11c ◴[] No.45271152[source]
It also completely negates their utility, even though the exact "problem" they always bring up is already solved when you use normal constructors.
replies(1): >>45272861 #
1. jcelerier ◴[] No.45272861[source]
Not sure I understand, since they're available in c++ designated initializes are one of the features I use most, to the point of making custom structs to pass the arguments if a type cannot be changed to be an aggregate. It makes a huge positive difference in readability and has helped me solve many subtle bugs ; and not initializing things in order will throw a warning so you catch it immediately in your ide
replies(1): >>45276932 #
2. o11c ◴[] No.45276932[source]
The problem is that there are a lot of APIs (even system and system-ish ones) that don't want to specify the order of their fields (or outright differ between platforms). Or that can't use a meaningful order due to ABI compatibility, yet the caller wants to pass fields in a meaningful order.
replies(1): >>45280829 #
3. jcelerier ◴[] No.45280829[source]
platform APIs like this are likely much less less than 1% of the things I call in my codebases. The few files i have open right now have absolutely no such call.