
C++ confessions of a C developer - wyldfire
http://undo.io/resources/blog-articles/cpp-confessions-of-a-c-dev/
======
minipci1321
People criticising C++ always sound to me like the language somehow leads them
into abusing it.

People mess up when coding in C++, not the other way round. Blaming the tools
and seeking restrictions to one's freedom of expression in using them, won't
have my sympathy. At one shop I once worked, they have added all sorts of
protections to all tools they used, in order to prevent developers from doing
wrong. For example, in the source configuration management, they disabled
branching and parallel development, someone considered that too complicated.
And Make and its language was completely hidden, of course.

I personally greatly value C++ for the expressive power it gives, compared to
straight C -- not only solve the problem but express the algorithm the way
that a random person reading the code instantly grasps the idea, etc. I find
it harder to achieve using straight C, on average. (I am in low-level and
performance-related development, choice is frequently only between the two.)

~~~
Klockan
The bigger the team the more restrictions you want in a language.

C++ is lovely when you are working alone, but as the team grows people start
having very conflicting opinions on what parts of the language should be used.
The only way to handle this is to restrict everyone to a subset of C++ and
forbid everything else, otherwise it will be impossible to navigate the
codebase.

Innocent looking things like try/catch becomes a nightmare when not everyone
has taken that into consideration when they clean up resources. Either you
have to enforce proper cleanup around every function call or you have to
forbid everyone from throwing exceptions, there is no middle ground here.

~~~
pjmlp
Does anyone know everything about Python 3.5, language and libraries,
including evolution issues when having to handle old code?

What about style guides to forbid people to use generators, metaclasses or
other advanced features?

Just to pick a language most people think it is easier than C++.

~~~
witty_username
Are generators really an advanced feature? I've used them pretty often
(instead of returning a list) and they're pretty simple.

~~~
pjmlp
For many types of programmers that would be happy with something like C or Go,
most likely yes, given that they already allow for some interesting patterns.
Also because they changed a bit, in the early days you could not pass
arguments back to them.

However, when I wrote generators I was actually thinking about comprehensions.

~~~
witty_username
I think comprehensions are less advanced (they're a specialized case of
iterators) and very commonly used.

> Also because they changed a bit, in the early days you could not pass
> arguments back to them.

Sending arguments is certainly advanced.

------
clishem
Incredible book (and reference) if you want to learn literally everything
about C++ (and which parts to use, how and why):
[http://www.icce.rug.nl/documents/cplusplus/](http://www.icce.rug.nl/documents/cplusplus/)

It's continually updated (pdf available on GitHub).

------
userbinator
Personally, unfortunately the more exposure I've had to the "Modern C++"
community, the more I feel like it's dogmatic, cargo-cult, and obsessed with
"best practices", which really puts me off. It looks like a manifestation of
the second-system effect compared to C, which I'd characterise as a simple,
pragmatic, and humble language. Then again, I've always thought OOP was
oversold and that may have something to do with it.

That is not to say I fundamentally dislike the language itself; C++ certainly
has some very useful features compared to C, and I sometimes wish I could make
use of them when I'm writing C too, but the "C++ is so much better than C, C
is bad" and all-too-common "some bloke named Herb says X, so you must follow
him" sort of attitude coming from self-proclaimed "Modern C++" developers I
find rather distasteful. I don't care about purity or adherence to any
perceived "expert opinion", I just want to use the features of the language
that make it easier and simpler to solve a problem more efficiently (and
likewise, _not_ use the features which don't help)... which is what
programming languages ultimately are for, and yet I feel much of the "Modern
C++" community has missed that point.

~~~
petters
I suppose that you could just use C with std::vector and std::unique_ptr from
C++, and you’d be much better off. The you could still write "C" code but not
worry about memory leaks.

Of course, you also need std::sort because it is much faster than qsort.
Thinking about it, you probably want to implement all time-critical algorithms
with generics.

... never mind, this quickly becomes a stone soup.

~~~
bluecalm
I keep hearing that std::sort is faster than qsort yet when I benchmarked it
with GCC it was the exact same performance which makes sense as modern
compiles are either to optimize function calls through pointers. Anyway, both
are significantly slower than macro based library based on removing function
calls altogether (not the one in comparator but the recursive qsort calls).

For things I would like to use C for std::vector is not very useful either as
it grows in some generic way which is likely wasteful for specific
applications. If an algorithm is time-critical then almost by definition the
generic implementation in standard library won't be enough.

~~~
gpderetta
> removing function calls altogether (not the one in comparator but the
> recursive qsort calls).

That's an impressive feat as quick sort is not tail recursive.

Edit: you can of course maintain the recursion stack by hand but that's not
normally a win.

~~~
bluecalm
I am not sure why you think it's not normally a win. It is quite a big win in
qsort implementation at least.

------
tluyben2
After many years of writing a self confessed horrible mix of C/C++ and calling
it C++ (and I think more people have done that), I decided to learn C++11 and
then C++14. I am enjoying it quite a bit I must say... Anyone else have that
experience?

~~~
revelation
I made a conscious choice to use some of the new stuff like unique_ptr
(primarily) and shared_ptr for memory management w/ move, lambdas, range loops
and forward for varargs functions that are not an unsafe macro mess,
constexpr, static asserts. It was quite nice, some of that is due to compiler
progress, i.e. I always run with address sanitizer now.

Sadly they seem to have added more than they cleaned up. It's 2016, I don't
want to have to write another freaking include guard. That should have ended
years ago. UTF8 and friends are also a consistent pain, mostly because 3rd
party library support is absent. This requires a strong message from the
standard library or it's always going to falter.

~~~
mrich
pragma once is supported by enough compilers to be considered a standard by
now :)

~~~
slavik81
I built guardonce[1] to help mitigate complaints that #pragma once was non-
standard. If you can easily convert back to include guards, you can worry less
about what compilers you might support in the future and just consider the
compilers you support right now.

[1]: [https://github.com/cgmb/guardonce](https://github.com/cgmb/guardonce)

------
bogomipz
I have a question. I often see titles of blog posts or books such as "Learn
Modern C++" or "Learn Modern Java" but how possible or practical is this for a
newcomer when the languages have been around this long?

For instance what happens if you opt for the strictly "modern" just Java 8 or
C++17 type approach to learning these. And then you end up as the case in the
real world needing to maintain legacy code? Or you collaborate with people who
have been using the language for a long time but don't use these modern
approaches?

~~~
aidenn0
"Modern C++" at least partly refers to generic programming techniques that
were popularized by Alexandrescu's book "Modern C++ Design"; a lot of the
features in C++11 improve language support for such techniques.

The term also implies use of RAII[1] which is a technique that was used at
least a decade before C++ was an ISO standard.

The third implication is a rejection of what is often called "C+" or "C with
classes" which is the treatment of C++ as C with a couple extra features.
Since a lot of early C++ programmers were C programmers, and a lot of early
C++ compilers were fairly mediocre in their implementation of language
features[3], the 90s generated a lot of "C+" code.

Really the first two points are a subset of this one as they are two prominent
examples of programming techniques that are fairly foreign to C, but are
extremely valuable for writing robust C++ code.

So "Modern C++" really means "Something widely considered a good idea for
about a decade" at this point.

If you work on a good legacy code base, the typical old oddness will be
"Version X of MSVC/GCC/whatever had poor RVO[2], so we did this ugly thing for
optimization" and (obviously) either a complete lack of using the features in
C++11 or using a proto-version of them via Boost.

Even the nicest legacy code bases will have really odd things whose original
motivation would require serious archaeology to divine, but that happens even
with relatively static languages.

1:
[https://en.wikipedia.org/wiki/Resource_acquisition_is_initia...](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization)

2:
[https://en.wikipedia.org/wiki/Return_value_optimization](https://en.wikipedia.org/wiki/Return_value_optimization)

3: Believe it or not, a lot of the code from Alexandrescu's book could not
compile in either MSVC _or_ GCC when it was published. The library that
accompanied the book became incorporated into many compiler vendor's
conformance testing.

~~~
bogomipz
These were all great comments thanks. Would people still suggest
Alexandrescu's book "Modern C++ Design" as the best source for newcomers to
learn modern C++ then?

~~~
pjmlp
It is one source yes.

But you should also have a look at "Tour of C++" and "Elements of
Programming".

Also Microsoft has lots of Modern C++ talks on Channel 9 done by Stephan T
Lavavej.

[https://channel9.msdn.com/tags/STL/](https://channel9.msdn.com/tags/STL/)

~~~
bogomipz
Thanks for the recommendations. Cheers.

------
Tempest1981
Is anyone using Stroustrup's C++ Core Guidelines? And GSL?
[https://isocpp.org/blog/2015/09/bjarne-stroustrup-
announces-...](https://isocpp.org/blog/2015/09/bjarne-stroustrup-announces-
cpp-core-guidelines)

They look like a way to add some safety to pointers and memory allocation, for
those who demand bare-metal performance. Wondering if it's taking off...

------
bogomipz
Does anyone know if the videos from this conference have been posted
somewhere? The slides are. The talks referenced in the author's post sound
really good.

~~~
sideb0ard
They have a channel here, looks like they're still in the process of uploading
the talks -
[https://www.youtube.com/user/MeetingCPP/videos](https://www.youtube.com/user/MeetingCPP/videos)

~~~
bogomipz
Oh brilliant. Thanks!

