Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Would someone mind giving a description of "writing C in C++"?

It seems a bit off to me to claim it's a superset of C while banning the subset of C.

Like it or not, C++ does have things like raw pointers, null terminated arrays, setjmp/longjmp, and macros. That's part of being a superset of C. Using them in C++ is still C++ if it's compiled by a C++ compiler.

It may not be idiomatic, but I'd love to see the C++ community come up with a single definition for idiomatic C++. And to preempt the answer of "idiomatic C++ is Modern C++" -- How about things like exceptions, rtti, and iostream?



> Would someone mind giving a description of "writing C in C++"?

For starters, write non-idiomatic code which employs (exclusively or not) a subset of features.

Imagine writing Python and only using free functions, while vehemently refusing to use stuff like classes or list comprehension or exceptions or anything at all. Similar to "writing C in C++", this would be "writing shell scripts in Python".

> Like it or not, C++ does have things like raw pointers, null terminated arrays, setjmp/longjmp, and macros. That's part of being a superset of C. Using them in C++ is still C++ if it's compiled by a C++ compiler.

You're missing the whole point.

"Writing C in C++" is synonymous to "not using new C++ features that are safer and faster and better than the old C features".

It's completely irrelevant if the compiler supports raw pointers or preprocessor macros. The whole point is that, say, intentionally using raw pointers when you have smart pointers is hardly justifiable, and you're not doing yourself any favor.


I can understand if people want to avoid using classes. But list/dict/set comprehensions make building complex data structures easier and more performant.

Usually, if one avoids OOP features of Python, that means he/she likes to use more functional features. And vice-versa.

I imagine it's the same with C vs C++. People use malloc/free, classic for loops and structs+functions only because that style fits their mindset better.


Bjarne even gave an example at the talk with span versus pointer and length as argument.

Writing C in C++ means exactly that, writing C++ code in the subset that is also valid C, while ignoring everything else that the language offers regarding type safety, including basic stuff like RAII, or templates instead of macros.

Technically it is C++ code, in practice can be compiled with a C compiler as well.


> It seems a bit off to me to claim it's a superset of C while banning the subset of C.

C++ is not a superset of C.


Unfortunately, I'm very well aware




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: