Comparison and Inheritance -- Andrew Koenig
Comparison and Inheritance
by Andrew Koenig
We continue last week's discussion of comparison functions by thinking about how to compare objects from different parts of an inheritance hierarchy...
November 14-16, Berlin, Germany
November 18-23, Wrocław, Poland
November 25, Wrocław, Poland
February 10-15, Hagenberg, Austria
March 19-21, Madrid, Spain
April 1-4, Bristol, UK
June 16-21, Sofia, Bulgaria
By Blog Staff | Feb 25, 2013 03:11 PM | Tags: intermediate
Comparison and Inheritance
by Andrew Koenig
We continue last week's discussion of comparison functions by thinking about how to compare objects from different parts of an inheritance hierarchy...
By Blog Staff | Feb 25, 2013 08:55 AM | Tags: basics
People new to C++11 often hear about move semantics, and expect that they have to do work to take advantage of it. That's often not true, and often the cleanest, simplest code that doesn't even mention move
or &&
anywhere is just what you want -- that's C++11, clean, safe, and faster than ever.
Perhaps the most common case (and question) involves returning values from functions. The new rule for modern C++ style: Just return even big objects by value, and move Just Happens.
It just came up again on StackOverflow:
C++11 rvalues and move semantics confusion
The link skips straight to Howard Hinnant's clear and correct answer.
Sometimes we just try too hard, because we expect efficient programming not to be easy. Welcome to C++11.
By Blog Staff | Feb 21, 2013 09:25 AM | Tags: intermediate
To accept a functor as a parameter, when should you:
std::function
, which adds an indirection, vs.template<class Func>
and accept a Func
, which can bind directly to whatever is passed?std::function vs template
Thanks to C++11 we received the
std::function
family of functor wrappers. Unfortunately, I keep hearing [...] that they are horribly slow. [... Is the right recommendation] thatfunction
s can be used as de facto standard of passing functors, and in places where high performance is expected templates should be used?
By Blog Staff | Feb 19, 2013 04:39 AM | Tags: basics
Quick A: Using the form of insert
that takes an rvalue and passing a temporary or a std::move
'd object, or calling emplace
.
Moving an object into a map
The problem with this is that the huge objects will be copied into the maps
Huge huge1(some,args); Huge huge2(some,args); std::map<int,Huge> map1; std::map<Huge,int> map2; map1.insert({0,huge1}); map2.insert({huge2,0});how can I guarantee a move? Will this work or is there more to it?
map1.insert({0,std::move(huge1)}); map2.insert({std::move(huge2),0});
By Blog Staff | Feb 18, 2013 05:32 PM | Tags: intermediate basics
Quick A: Yes. Another way that modern C++ is safer and simpler.
(If you don't know what the safe-bool idiom is, don't worry. It's a workaround that's now obsolete.)
Xeo asked:
Is the safe-bool idiom obsolete in C++11?
This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple
explicit operator bool() const;... Is our assumption in the title correct? I hope we didn't overlook any potential drawbacks.
By Blog Staff | Feb 15, 2013 10:38 AM | Tags: intermediate basics
Andrzej continues this month with more interesting thoughts on preconditions.
Preconditions, Part 2
by Andrzej Krzemieński
In this post I will continue sharing my thoughts on preconditions. It will cover some philosophy behind the concept of preconditions (and bugs), and investigate the possibility of employing the compiler to verify some preconditions. Many people provided a useful feedback on my previous post. I will also try to incorporate it into this post.
Note that this article diverges from recommended practice in one way... it hints at the idea of throwing exceptions to report precondition violations. Instead, per C++ Coding Standards and other established guidance, prefer to use assertions to check preconditions: precondition violations are just bugs in the caller's code that should be caught at test time, assertions cause no overhead in production, and assertions fire immediately at the line of code that contains the bug without losing the call stack and other local context. Using assertions is still considered to be a best practice.
By Blog Staff | Feb 8, 2013 09:36 AM | Tags: basics
The final installment of Bjarne Stroustrup's four-part Tour of C++ is now available. This material is a preview draft of Chapter 5 of Stroustrup’s upcoming The C++ Programming Language, 4th Edition.
A Tour of C++, Part 4: Concurrency and Utilities
by Bjarne Stroustrup
Bjarne writes:
Describe all of C++ in 100 pages (or less). Don't just describe the language, include the standard library. Don't use "white lies" to simplify. Describe the major programming styles and techniques. Give rationale. Don't forget about concurrency. And, oh, by the way, make it readable to programmers (do not require a PhD).
That was the task I set myself when I decided to write the "Tour of C++" for TC++PL4. I suspect that succeeding perfectly is beyond me, but at least I met the first criteria: The tour is currently 98 pages, and shrinking.
This last, part 4, of the tour presents concurrency and some of the newer standard-library facilities.Enjoy!
See the whole Tour here.
By Blog Staff | Feb 7, 2013 08:13 AM | Tags: basics
Here's a nice intro and overview of one of the smaller features that makes C++11 safer.
Speaking of scoped and based enums, here's an interesting historical tidbit you may not know: These were initially co-proposed for C++ by an expert working on mission- and life-critical software and a large horizontal software company. Just goes to show the broad applicability of features like these, that matter where safety is critical and also help everyone.
Closer to Perfection: Get to Know C++11 Scoped and Based Enum Types
by Danny Kalev
C++ enum types pack a set of related constants in an intuitive and efficient user-defined type. Can you ask for more? With two new C++11 enhancements, namely scoped enums and based enums, the answer is "yes." Find out all about the recent facelift that C++11 enums underwent and learn how to refactor your code to benefit from the new enum features – without sacrificing performance or backward compatibility.
From the intro:
Enums are one of my favorite C++ features. They exemplify the notion of an efficient user-defined type without the heavy machinery of virtual functions, constructors, etc. (Compare C++ enums to other programming languages that still insist on using a full-blown class instead, and you’ll see what I mean.)
Yet, traditional enum types aren't flawless. ... C++11 addresses these issues with revamped enumerations that give you tighter control over the scope, size, and implicit conversions of enum types. Let's look at these new features more closely, and examine how they can improve both our code quality and frustration level.
By Blog Staff | Feb 5, 2013 02:28 PM | Tags: intermediate
A short useful Q&A about how two C++ features interact:
C++11 Delegated Constructor Pure Virtual Method & Function Calls -- Dangers?
In C++ 11, what are the dangers of invoking Pure Virtual functions in a class' constructor, during construction, but after the class/object has been "fully constructed" via constructor delegation?
...
By Blog Staff | Jan 31, 2013 08:18 AM | Tags: basics
Quick A: It's a valid object with an unspecified state, so start by using member functions that have no preconditions. For example, assign a new value to the object.
What can I do with a moved-from object?
Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient...