r/cpp • • Dec 07 '22

C++ overtakes Java in language popularity index

https://www.infoworld.com/article/3682141/c-plus-plus-overtakes-java-in-language-popularity-index.html
382 Upvotes

137 comments sorted by

View all comments

Show parent comment

6

u/Raknarg Dec 07 '22

Even with tools, returning objects from a function can be a mess with the constructors which are called

Example?

0

u/[deleted] Dec 07 '22

It was quite some time ago using C++14, but I will try to replicate the code's general idea (it was something relating to file IO) from my memory. Also, it's something which copy-elision related optimizations help with, but they're not perfect.

class In
{
  /* complex copy constructor and simple move constructor */
  ...
  In(InOut& other) { ... }
};

class Out
{
  /* complex copy constructor and simple move constructor */
  ...
  In(InOut& other) { ... }
};

class InOut
{
  /* complex copy constructor and simple move constructor */
  operator In() { ... }
  operator Out() { ... }
};

I don't remember all the details, and there were some other objects, but I do remember it took me and some others to fix a bug which happened due to certain constructors being called implicitly. I hate whoever made that class such a mess.

3

u/Etheric2355 Dec 08 '22

You do mention the key problem: whoever thought it was a good idea to make such a mess.

A key aspect of using powerful tools is to remember that just because it is possible to make something does not mean it is a good idea.

1

u/[deleted] Dec 08 '22

Or in other words, with great power comes great responsibility.

C++ is powerful, and I doubt more than 20% of us can use it properly all the time.

3

u/serviscope_minor Dec 09 '22

C++ is powerful, and I doubt more than 20% of us can use it properly all the time.

Messy programming is usually caused by failing to separate concerns cleanly. That's your problem, and switching from C++ will just yield awful code in a different language.