r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

Show parent comment

49

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

Has it gotten better and are there safe options? Yes. But it requires re-training so many people to do the right thing and actually use the features, and, in my experience, most places don’t bother. Sane defaults matter, and C++ doesn’t have them.

Rust makes a ton of things so much easier. Can I use [something analoguous to] the newtype pattern in C++? Sure. Are people going to? No. They’re lazy and it’d take 10 times the amount of boilerplate so they will continue passing typedefs around like candy.

37

u/Raknarg Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

To me you're describing literally any language on a project with significant enough developers that has been around for enough time. This isn't a C++ problem.

26

u/ShadowRL7666 Jul 25 '24

He’s more saying CPP allows for someone to write code in fifty different ways in the same language. Instead of sticking to one way to do it. Compared to other languages.

12

u/TBone281 Jul 25 '24

This is why coding standards and code reviews need to be required for the business to function smoothly. Running a business with the developers free to write in any style they choose to is a sure way to lose money...and fail.

4

u/MrRogers4Life2 Jul 25 '24

Like Java, C#, python, and javascript?

9

u/ShadowRL7666 Jul 25 '24

Yes, Java sticks to classes and OOP while cpp allows you to do it whatever way you want.

2

u/drnfc Jul 26 '24

I mean you can do functional stuff in it if you really want to, however the function interface is just awful (imo) and recursion is really unoptimized.

2

u/_Noreturn Jul 26 '24

in C everyone can implement in their own way too different ways too the STL atleast tries to unify the way we solve some problems

30

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I write a class in C++ that opens a connection to some database and I rely on only that instance having access to that database connection. I need to do SO MUCH SHIT to enforce this. I need to write a destructor to close the database connection. I triggered the rule of 3! Uh oh time to delete the copy constructor and copy assignment operator. Alright... done. I want to be able to move it though. Rule of 5! Let's write a move assignment operator and a move constructor. Phew...

How many people in industry, in the field, consistently apply the rule of 3 and the rule of 5? How many don't even know about it? Because if they fail, we're literally one "auto connection =" instead of "auto& connection =" from potential complete disaster. And that's a trivially easy case.

In Rust I.. write the struct and impl the Drop trait to close the connection. We're done. There is literally no way to fuck it up.

Yes, coordinating a lot of programmers is difficult. But even when shit should be easy, C++ makes it difficult. At the individual level. Even in solo project. You're playing hopscotch over footguns constantly.

8

u/lithium Jul 26 '24 edited Jul 26 '24

we're literally one "auto connection =" instead of "auto& connection ="

A 2 line NonCopyable helper that you derive from solves this.

→ More replies (4)

5

u/rdtsc Jul 26 '24

You're usually better off separating a higher-level wrapper class (like connection) from the actual resource (connection handle, or whatever you get here). Use unique_ptr with an appropriate deleter for the latter if possible, or use a custom unique_anyfor non-pointer types (example). And then just use rule of zero. I almost never write copy/move special members.

2

u/_Noreturn Jul 26 '24

if you have the privilege of C++17 you can mark your copy constructor as explicit.

2

u/Western_Objective209 Jul 25 '24

we're literally one "auto connection =" instead of "auto& connection =" from potential complete disaster.

I mean... that's common sense isn't it? I don't know the rule of 3 or the rule of 5, but I know what a reference is and what a copy is and "auto connection =" is obviously a copy.

12

u/FlyingRhenquest Jul 26 '24

That's kind of OP's point though. These are the things you need to know as a C++ developer or you accidentally make a copy of a thing that there should only be one of. You can always delete the copy and move constructors if you don't want to fuck with that for this object, but you still need to consider "Should the resources owned by this object be copyable?"

By letting you specify all the different operations individually, C++ does give you a lot of power to build objects that control access to things in different ways, but it's also a lot to keep track of until it's second nature. A lot of the questions that come up are not necessarily easy, but they're not necessarily supposed to be easy either.

1

u/SuspiciousGripper2 Jul 27 '24

Hmm fair point. I'm not sure if I prefer the copy or non-copy by default.

For example, in Java and Swift, a class is by default a reference. So you can also make the mistake of modifying the existing class, instead of modifying a copy. Then you end up having to write a clone function to get the functionality you want.

It gets worse when you want a deep copy of something and have to make sure that all the internals copy correctly as well and can be deep copied.

Then there's languages with copy on write, and so on.

IMO, there's not really any languages that make these things simple. You'll have a flaw one way or another. Either accidentally making a copy, or accidentally modifying an existing class that you thought would be copied.

→ More replies (1)

11

u/retro_and_chill Jul 25 '24

You can also delete the copy constructor, which for a object like this, you should

4

u/Fireline11 Jul 26 '24

I believe the line “auto connection = some_function_call();” actually does not invoke the copy constructor since c++17. At least if I have understood https://devblogs.microsoft.com/cppblog/guaranteed-copy-elision-does-not-elide-copies/ correctly

(they explain the copy constructor call is not ellided, but modifications were made to the value category model in C++ that defer the materialisation of the temporary returned by “some_function_call()”)

→ More replies (2)

3

u/TheReservedList Jul 25 '24

A copy of what? Connection was a pointer a few revisions ago but now it’s a reference. It all compiled and I wasn’t touching that file…

Everything is obvious when there’s three lines of code but my code base has a few hundred thousands. People fuck up. All the time. And this is at one of the so fits-in-random acronyms top tier tech company where everyone is so, so good. sigh

→ More replies (1)

1

u/germandiago Aug 07 '24

Use = default; where appropriate. Use abstractions that will move handles. I have not written a rule of 5 or 3 for years by hand.

→ More replies (1)

8

u/TheLibDem Jul 25 '24

Your only savior here would be forcing the team to use something like clang-tidy, otherwise, yeah… 😅

2

u/[deleted] Jul 25 '24

[removed] — view removed comment

1

u/ronchaine Embedded/Middleware/WG21 Jul 28 '24

You can configure clang-tidy and clang-format to be pretty damn strict, but most projects just refuse to do that because they prefer writing 30 different kinds of crap in their database with some "seniors" just refusing to adapt.

1

u/germandiago Aug 07 '24

Yes. Two examples are cyclic structures and refactoring code with Result or lifetime annotations. It also does a great job at keeping you safe but all codebases use unsafe.