Re:Bartosz Milewski - The Downfall of Imperative Programming
by fferen on Monday April 09, 2012 @08:20PM
(#3820011)
I've only been using C++ for a few months, but it's my opinion that all function parameters should have been "const &" by default (except for maybe primitive types). Const correctness has helped me spot some nasty bugs, and it's too easy to accidentally forget the "&" and pass horrendously large structures by value, which must go through a copy constructor and destructor with every call. Plus, it gets really tedious typing "const Mat &src" a million times (guess which library I'm using, lol).
[ Parent
| on HN ]
Re:Bartosz Milewski - The Downfall of Imperative Programming
by octopus on Monday April 09, 2012 @09:20PM
(#3820208)
In C++11 "type const &name" can be simply replaced with "type name", no performance penalty involved.
Re:Bartosz Milewski - The Downfall of Imperative Programming
by ajax77 on Monday April 09, 2012 @11:37PM
(#3820549)
That's simply not the case.. In general, the "no penalty" holds true for primitive data types, and when passing an r-value instance of a class that has a move copy constructor defined. In the case where your object is an expensively constructed l-value, it is preferable to pass by const reference rather than value (assuming you plan to use the object later... otherwise you could indeed pass using std::move semantics).
Re:Bartosz Milewski - The Downfall of Imperative Programming
by octopus on Tuesday April 10, 2012 @02:02AM
(#3820931)
In C++11 you are supposed to use move semantics in your own classes. I'm not 100% sure but I think the standard require that the STL use move semantics.
Re:Bartosz Milewski - The Downfall of Imperative Programming
by cageface on Tuesday April 10, 2012 @08:04AM
(#3821761)
Yes but this is one more thing you have to think about when implementing a class in C++ and one more chance to screw it up.
It makes vanilla C and raw pointers look a lot more appealing, to be honest.
Re:Bartosz Milewski - The Downfall of Imperative Programming
by octopus on Tuesday April 10, 2012 @10:00AM
(#3822169)
Actually I think C++11 is a simpler language than C++03 or C++98. The problem is that we need to un-learn what we know and learn the new way of doing things.
Re:Bartosz Milewski - The Downfall of Imperative Programming
by cageface on Tuesday April 10, 2012 @09:49PM
(#3825526)
C++11 is simpler for the casual user of classes but I'm not so sure it's easier if you're implementing a lot of classes yourself. I disable assignment and copying in all my classes by default unless I have a really good reason to implement them and I still pass by const & for types I define myself.