News for hackers. Stuff that matters.
Re:Why I don't spend time with Modern C++ anymore by hellofunk on Wednesday May 18, 2016 @05:42AM (#11720757)
This article is not very general. Much of what it tries to convince us is not going to matter for most developers, and has the cost of suggesting modern features are not good for any developers. For example:

>It is not rare to see Modern C++ applications taking 10 minutes to compile. With traditional C++, this number is counted in low seconds for a simple change.

This is simply a bogus statement with respect to what at least 90% of c++ developers do on a daily basis.

I have benchmarked unique_ptr, auto, brace initialization, lambdas, range-based-for and other modern idioms and found them all to be at least as fast, and often faster, than their older counterparts. Now, if I were to instead go off and write template-heavy code using new features, that would be different. But in reality, the vast majority of c++ developers -- I'd wager at least 95% -- are not writing variadic templates on a daily basis (nor should they be).

The memory safety and many benefits from unique_ptr [0] is one of many modern tools that is a non-brainer to use in nearly all contexts. No, not nearly all contexts, allow me to rephrase: all contexts. It just is, and if you compare its use to manual new/delete code, the benefits are solid and faster.

The author further claims that modern C++ is less maintainable and more complex. The absolute opposite is true in nearly all cases. Using unique_ptr again as an example, it leads to less code, less complex code, more clear code, and better maintainability and code readability. Uniform brace initialization is another example that prevents many common older problems in the language.

FYI the author keeps talking about high frequency trading as an example of why modern c++ is a bad choice. Well, I worked at a HFT firm for a long time until last year, the firm places millions of trades per day and is among the most successful in the markets it trades. And what did we use? Only modern features. Lambdas, auto, unique_ptr, range-fors, even std::async -- everywhere in our code. This author is either naive or political.

I think the title of this article is highly misleading, and the contents are not relevant. Overall, this article is just bad advice for most of us.

[0] https://news.ycombinator.com/item?id=11699954

Re:Why I don't spend time with Modern C++ anymore by physguy1123 on Wednesday May 18, 2016 @08:58AM (#11721539)
> FYI the author keeps talking about high frequency trading as an example of why modern c++ is a bad choice. Well, I worked at a HFT firm for a long time until last year, the firm places millions of trades per day and is among the most successful in the markets it trades. And what did we use? Only modern features. Lambdas, auto, unique_ptr, range-fors, even std::async -- everywhere in our code. This author is either naive or political.

Just an anecdote, but I also work at a successful HFT as well modern C++ is almost everywhere, without any performance hits and much cleaner code. Often it's easier to write fast code with it.

Re:Why I don't spend time with Modern C++ anymore by gpderetta on Wednesday May 18, 2016 @08:15AM (#11721336)
"Well, I worked at a HFT firm for a long time until last year, the firm places millions of trades per day and is among the most successful in the markets it trades. And what did we use? Only modern features. Lambdas, auto, unique_ptr, range-fors, even std::async -- everywhere in our code. "

FWIW, I have been on the industry only 3 years so far, but I also see modern C++ and boost everywhere. I'm aware of the obvious selection bias, but then again it also applies to the author: if a firm has to call an external consultant to make sense of their codebase, it wasn't probably very good to start with.

Re:Why I don't spend time with Modern C++ anymore by daemin on Wednesday May 18, 2016 @06:05AM (#11720830)
I got the feeling that the author's beef with Modern C++ is the same as for any C++ that is template heavy. And to me it is not a new or unique experience (i.e. not limited to Modern C++) to have builds that take 45 minutes to build everything. It can happen in older code and in newer code.
Re:Why I don't spend time with Modern C++ anymore by adrianN on Wednesday May 18, 2016 @06:11AM (#11720846)
Yes, templates make the compiler work harder (sometimes at least), but badly modularized code with "big ball of mud" dependency structure also makes the compiler work a lot. You don't need fancy features to do that.
Re:Why I don't spend time with Modern C++ anymore by adrianratnapala on Wednesday May 18, 2016 @07:14AM (#11721089)
"Big ball of mud" dependency structures are common because the most pragmatic quick-fix for so many compile errors it to just whack in a new #include.

C++ is worse than most languages in this way, because private members are defined in public headers. This forces you add #includes to the headers. Thus the #include graph is much denser than the true dependencies in your program.

There's tricks for getting around this, but they aren't always practical; and they go against the grain of the "every class has its own .cpp and .h file" rule, which lots of people want to insist on.

Re:Why I don't spend time with Modern C++ anymore by leni536 on Wednesday May 18, 2016 @07:39AM (#11721184)
>There's tricks

You mean PIMPL?

Re:Why I don't spend time with Modern C++ anymore by adrianratnapala on Wednesday May 18, 2016 @08:06AM (#11721301)
Yes, I think so, though I was never truly clear on what PIMPL was supposed to mean.

I am referring to writing a base class in the header and then doing the actual implementation in a derived class visible only inside one .cpp file. There are some other tricks you can do too, such as using pointers-to-incomplete types.

When I do any of these tricks, find that someone else later comes and makes a changes that introduces the extra #includes after all.

Re:Why I don't spend time with Modern C++ anymore by hellofunk on Wednesday May 18, 2016 @08:18AM (#11721348)
PIMPL means "pointer to implementation" and the reason you and your colleagues are having difficulty with it is because you don't understand it, and therefore should not be using it, or, investing the time to understand it before trying to use it.

>every class has its own .cpp and .h file

Wrong. There are so many high-quality and widely-used libraries that are "header only".

>When I do any of these tricks, find that someone else later comes and makes a changes that introduces the extra #includes after all.

It is possible to write sloppy code in any language, and also to receive low-quality contributions from anyone else to your code. This is not unique to C++. That's why it is important that everyone in a team be on the same page about how code is organized.

>I am referring to writing a base class in the header and then doing the actual implementation in a derived class visible only inside one .cpp file.

This is not the PIMPL idiom, though it is halfway right.

Re:Why I don't spend time with Modern C++ anymore by aspiringuser on Wednesday May 18, 2016 @04:54PM (#11725870)
PIMPL pattern is great for reducing compilation time and creating stable user-facing APIs but it has a runtime cost. The "implementation" part is usually heap allocated and calling its methods have extra overhead of crossing the pointer to implementation barrier.
Re:Why I don't spend time with Modern C++ anymore by hellofunk on Thursday May 19, 2016 @02:54AM (#11728524)
With the use of link-time optimization, most of the overhead of using pimpl idiom is removed. Both the outter wrapper functions as well as the implementation functions can be inlined, making the function calls effectively like a normal class implemented inside a header.
Re:Why I don't spend time with Modern C++ anymore by aspiringuser on Thursday May 19, 2016 @06:56AM (#11729259)
The heap allocation for the implementation won't be removed by LTO.
Re:Why I don't spend time with Modern C++ anymore by hellofunk on Thursday May 19, 2016 @07:27AM (#11729368)
I'm inclined to agree that for objects that are going to be created and destroyed very often, especially in tight loops, PIMPL is not an ideal idiom. I wouldn't use it to generate fleeting effects in graphics, for example.

For objects that are used in setting up parts of a system and that will persist for long periods of time, maybe even the lifetime of the app, PIMPL is particularly well-suited for such classes. Classes that might manage networking, or encompass an entire part of app's running architecture, etc.

Re:Why I don't spend time with Modern C++ anymore by hendzen on Thursday May 19, 2016 @11:30PM (#11735399)
Thw complexity and overheead of PIMPL is justified in one scenario, and one scenario only - when you are writing a shared library and need to maintain a stable ABI. Otherwise it is complete overkill.
Re:Why I don't spend time with Modern C++ anymore by daemin on Wednesday May 18, 2016 @06:22AM (#11720894)
In my experience both have been true. That's why you get 10+ minute link times in some projects, so even if you use Incredibuild to compile, it still takes ages to link.
Re:Why I don't spend time with Modern C++ anymore by nmeofthestate on Wednesday May 18, 2016 @09:14AM (#11721657)
(re 10 minute compile times)

> This is simply a bogus statement

I work on a C++ project and believe me, 10 minute compile times would be great :)

Re:Why I don't spend time with Modern C++ anymore by hellofunk on Wednesday May 18, 2016 @09:58AM (#11721950)
The bogus part is 10 minutes vs a few seconds just because you change to the latest standard.
Re:Why I don't spend time with Modern C++ anymore by matthewaveryusa on Wednesday May 18, 2016 @10:08AM (#11722025)
have you tried ccache? the 5x to 10x improvement isn't a bogus statement in my experience
Re:Why I don't spend time with Modern C++ anymore by plorkyeran on Wednesday May 18, 2016 @10:46AM (#11722330)
ccache eliminates spurious rebuilds, but doesn't make compilation of things which actually need to be recompiled any faster. It's basically just a workaround for that Make only uses file mtimes and not the contents to decide what needs to be built.
Re:Why I don't spend time with Modern C++ anymore by matthewaveryusa on Wednesday May 18, 2016 @10:57AM (#11722414)
In theory, agreed. In practice I see a lot of re-compilation in large projects.
Re:Why I don't spend time with Modern C++ anymore by pjc50 on Wednesday May 18, 2016 @10:35AM (#11722245)
ccache doesn't help as soon as you change a popular header file. distcc helps quite a bit, but there was a project I worked on for years where by the end the compile time was 15 minutes after you'd distributed it across 20 machines.
Re:Why I don't spend time with Modern C++ anymore by fish2000 on Wednesday May 18, 2016 @12:07PM (#11722934)
“A Variadic Template A Day Keeps The Job Security In Play”
Re:Why I don't spend time with Modern C++ anymore by gpderetta on Wednesday May 18, 2016 @05:04PM (#11725954)
You should see the code that variadic templates replace.
Re:Why I don't spend time with Modern C++ anymore by hellofunk on Thursday May 19, 2016 @07:28AM (#11729370)
Say more! I'd like to know what you mean.
Re:Why I don't spend time with Modern C++ anymore by gpderetta on Thursday May 19, 2016 @09:08AM (#11729844)
Before variadic templates you couldn't define a function template taking an arbitrary number of arguments; what you could do is define N overloads up to a finite large N. This quickly becomes tedious and hard to maintain, so you either write an external generator script, or 'creatively' use the preprocessor. These solutions were also not much easier to maintain and of course would kill compilation times.

The worst part is that often these were forwarding functions, that is, they didn't do anything directly with their parameters, but simply forwarded them to some other function (this happen surprisingly often in highly generic code). To do forwarding correctly, you have to handle const and non const reference arguments properly, which means that N overloads are not enough and you need N*2^N (someone please check my math) overloads. As you can imagine, this was impractical for N > 3.

Variadic templates plus perfect forwarding via magic references make easy, practical and possible what was impractical before.

edit: spelling