←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 1 comments | | HN request time: 0.226s | 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 #
TuxSH ◴[] No.45273486[source]
> It really isn't. There's a lot of little nuanced differences that can bite you.

These are mostly inconsequential when using code other people write. It is trivial to mix C and C++ object files, and where the differences (in headers) do matter, they can be ifdefed away.

> 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++).

This makes sense because void* -> T* is a downcast. I find the C behavior worse.

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

As it should, but unscoped enums are promoted to integers the same way they are in C

> `char * message = "Hello"` is valid C but not C++

Code smell anyway, you can and should use char[] in both languages

You didn't mention the difference in inline semantics which IMO has more impact than what you cited

replies(2): >>45274231 #>>45279992 #
account42 ◴[] No.45274231[source]
> you can and should use char[] in both languages

Not for temporaries initialized from a string constant. That would create a new array on the stack which is rarely what you want.

And for globals this would preclude the the data backing your string from being shared with other instances of the same string (suffix) unless you use non-standard compiler options, which is again undesirable.

In modern C++ you probably want to convert to a string_view asap (ideally using the sv literal suffix) but that has problems with C interoperability.

replies(1): >>45274701 #
1. TuxSH ◴[] No.45274701[source]
Right, I've checked that char foo = "bar"; is indeed the same as the const char variant (both reference a string literal in rodata), which IMO makes it worse.

About string literals, the C23 standard states:

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
therefore `char foo = "bar";` is very bad practice (compared to using const char).

I assumed you wanted a mutable array of char initializable from a string literal, which is provided by std::string and char[] (depending on usecase).

> In modern C++ you probably want to convert to a string_view asap (ideally using the sv literal suffix)

I know as much