DEV Community

Cover image for Pitch me on C++
Ben Halpern
Ben Halpern Subscriber

Posted on

Pitch me on C++

Continuing the series! Feel welcome to dip in and weigh in on a past question.

Let's say I've never used C++ before. Can anyone give the run down of what the language does and why you prefer it? Feel free to touch on drawbacks as well.

Top comments (29)

Collapse
 
saptakbhoumik profile image
SaptakBhoumik • Edited

I will list the benefits of modern C++ because old C++ kinda sucks

  • It is very fast
  • It is extremely powerful and supports things like templates,compile and runtime time polymorphism,macros and much more
  • Easy interoperability with existing C libs
  • With things like shared_ptr and unique_ptr you no longer have to manually manage your memory
  • Supports both high level and low level devolopment

There are many more but these are my favorites

Collapse
 
pinotattari profile image
Riccardo Bernardini

Oh, well... May I change the question?

It is at least 20 years since last time I coded in C++. Maybe something changed; nevertheless, let me summarize my memories about it in two short phrases

  • C++: an OOP giant with clay feet inherited from C
  • C++: the power, the simplicity, the elegance and the safety of an hand grenade without pin.

I still remember the somersault with twist that I had to do to define a friend function of a template class...

It has been a long time, but some of the things that I remembered that I did not like

  • C-style #include that physically (rather than logically) embeds the included file. This has as consequences
    • You must use the ugly #ifdef INCLUDED hack.
    • If you use a use namespace in an header file, you pollute the namespace of whoever includes you
  • Template syntax too compact and obscure. Friends of class templates.
  • Automatic template expansion. At the time it looked like something cool, but with time I grew suspicious of compilers that try to be too smart. I prefer to write one line more to be sure to avoid obscure bugs.
  • Syntax a bit stricter than C, but not enough. Type system quite weak with automatic casting, still better than the "convert everything to everything else" of JS or PHP, but still too permissive for my tastes.
Collapse
 
pauljlucas profile image
Paul J. Lucas

t is at least 20 years since last time I coded in C++. Maybe something changed ...

Yeah, quite a lot. I respectfully submit that you learn modern C++ before commenting β€” unless you, for example, think comments about life in Italy under Mussolini's rule are still valid. Maybe something has changed in Italy since 1943?

Collapse
 
pinotattari profile image
Riccardo Bernardini

Well, it was pretty clear that my remarks were about the old version.

Although I have some difficulties to imagine how many of the defects I remember can be solved in new releases. Usually languages add features with time and drawbacks due to some issues in the original design (in this case, inheritance from C, obscure template syntax, automatic casting, type system C-like, ...) are difficult to solve because of the need of back compatibility.

Anyway, now I am curious and maybe I'll give a look to the new version, although I do not anticipate I will move to it soon.

Collapse
 
rfog profile image
rf og

"You must use the ugly #ifdef INCLUDED hack."

For this you have the non-standard "#pragma once", supported for most of modern compilers.

Collapse
 
aregtech profile image
Artak Avetyan

If you develop / deliver a library, you'd still better use old and nice #ifndef.

Collapse
 
pauljlucas profile image
Paul J. Lucas

And really modern compilers don't even need that.

Collapse
 
wesen profile image
Manuel Odendahl

I have a love hate relationship with C++.

  • Fast, zero abstractions, full memory control. This makes it one of the few fits for certain kind of programming: realtime / embedded / needing close control of memory behaviour
  • Has gotten quite nice with each successive standard since C++11
  • Strong and malleable type system. You can express many things with it.
  • Memory ownership is mostly a solve issue with smart pointers.
  • Decent IDE support with CLion (although you will need RAM)
  • Depending on where you are coming from, more accessible than Rust
  • Some amazing libraries out there (Qt), easy interop with C libraries
  • Great compilers

There's many things I could say about why I hate C++, but that's not what you are asking for.

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

One of my mentor who taught me C++ during my school days, used to say:

If you are not smart enough to code in C, you choose C++ .

Other than that, It has been 10 years I last wrote C++ during my college days, Few years ago, I was watching a youtube video on C++ and didn't know about auto type, and many other functionalities.

I am not qualified enough to pitch for C++ but I am subscribed to following channels to keep me updated on the new features of C++ programming languages. So for anyone interested:

  1. Cᐩᐩ Weekly With Jason Turner
  2. CppCast
Collapse
 
stojakovic99 profile image
Nikola Stojaković

Horrible. That's like saying you're a bad builder because you're using machines for building a flat instead of putting bricks on top of each other.

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

No. He was not saying that. He was saying, you are not bad builder because you are using machines, you are bad builder if you are using machines, without understanding the basic concepts like how to put bricks by hand. Because sometimes, to get something done properly, you need to use your hands.
He was a great teacher. And often used these types of jokes to keep us interested in Maths and programming.

First of all, we were in middle-schools, (5-7 class) in mid-90s in India. He wasn't insulting us or the languages. This joke made us focused on practical stuff, not just whats in syllabus. We wanted to learn about basics of programming instead of just satisfied with examples at back of our books. And our syllabus didn't cover a lot of concepts.

And we conceptually wasn't wrong, Memory management in C++ is relatively easier than C, but the amount of control that C provided meant that your skills can directly impact the quality of resultant code.

Thread Thread
 
stojakovic99 profile image
Nikola Stojaković

No. He was not saying that. He was saying, you are not bad builder because you are using machines, you are bad builder if you are using machines, without understanding the basic concepts like how to put bricks by hand. Because sometimes, to get something done properly, you need to use your hands.

That's okay, you should know the basics before you start using more advanced tools.

And we conceptually wasn't wrong, Memory management in C++ is relatively easier than C, but the amount of control that C provided meant that your skills can directly impact the quality of resultant code.

That's right, but on the other hand, C++ is much more complex language considering it has templates and other advanced constructs compared to C.

Collapse
 
tandrieu profile image
Thibaut Andrieu

It is an old language that is still very popular. This means 2 things:

  • You have a huge knowledge base: Forum, documentation, peoples, well established libraries, ...
  • You will still find these competencies in the next years to ensure maintenance of your product. It is not a new trend that will be obsolete in 6 months.
Collapse
 
zigrazor profile image
ZigRazor

Best Language for Perfomarce, Low Level Programming, Memory Management, Constraint on Object Oriented Programming and a Good Standard Library with so many function.
Obviously there is a minimum requirement... some years of experience and a good knowledge of the language, in the other cases there are a lot of possibilities to do worse than other languages.

Collapse
 
sandordargo profile image
Sandor Dargo

C++ has a rich past and a bright future.

Its past 40 years showed its value when it comes to writing low-level performance-critical code.

Its past 10 years also showed that it's not an old language staying with us for legacy reasons. The industry receives a new version every three years based on the rigorous processes of the standardization committee. These versions brought us things such as smart pointers or concepts that help us write similarly performant but easier to read, easier to write code as before.

The predictable schedules, the new features already in the pipeline and the rich ecosystem guarantee that C++ will stay with us for a long time not only as a must-use but as a want-to-use language.

Collapse
 
scottmichaud profile image
Scott Michaud

If you like C#, imagine if the compiler automatically inserted "using" for you. You never needed to remember to write it. C++ does that.

Lots of C++ features are about letting developer hide complexity (almost always from the caller). When used correctly, the higher level ("business logic") code can use instances of types and safely assume that they are doing the right thing. You can then compose those solutions to build up the actual solution that you're trying to do, relying upon each component to do the right thing. (This is not possible in garbage collected languages because you never know when or if the garbage collector will run.)

For a concrete example, imagine that you are writing a program that records your screen while a window is up.

In other languages, you will need to remember to stop the recording, otherwise you will corrupt the file. There may be several ways that the window can close, so you will need to make sure that function is called in every possible control flow.

In C++: Make the handle to the video recorder a field of the window (NOT the memory address of the handle, but the actual handle itself). When the window gets destroyed, it will destroy all fields. If the person who wrote the video recorder made sure to stop any pending recordings when its handle is destroyed, then the compiler will automatically call stop. When the window instance is destroyed, the video recorder has no-where to live, so it is destroyed, and its destruction process safely stops the recording.

The caller didn't need to think about this, even if a (normal) error is encountered.

(Normal error meaning that your program is still in control of execution. It won't help you if the OS forcibly stops your execution, or you encounter a power failure, etc. Basically, if a "finally" would have helped you in another language, then you can rely upon C++ just doing the right thing without the caller needing to remember anything.)

Collapse
 
simeg profile image
Simon Egersand 🎈

I've never written C++ so I'll be following this thread with excitement!

Collapse
 
ben profile image
Ben Halpern

subscribe

Hit the comment subscribe button if you haven't! Not everybody knows how that works, but it's great for threads you care about.

Collapse
 
simeg profile image
Simon Egersand 🎈

Today I Learned. Thanks, Ben!

Collapse
 
codewander profile image
codewander

able to write cross platform libraries that you can share almost everywhere - mobile, backend services, various desktop OS's.

Collapse
 
rfog profile image
rf og

IMHO, that is not completely true, to not to say absolutely not true.

Collapse
 
codewander profile image
codewander

With electron, react-native, react, and node, a js library might have further reach.

Thread Thread
 
rfog profile image
rf og

And what is the relation of that with C++ being "able to write cross platform libraries that you can share almost everywhere...". Each version of each library will need its own different C++ code base, more if they use UI, that is completely different and incompatible between platforms.

Thread Thread
 
codewander profile image
codewander

I just remember that one company that I worked at had an engine that would compute locations for items, and they wanted to share that engine between desktop apps and mobile apps. There was no UI in the engine. So, they ended up implementing it in a subset of c++ to be able to use the same source code for the engine in those disparate locations. I wasn't directly involved.

I was wondering if they would use js today.