Posts Tagged ‘rvalue reference’
If found a very nice article about rvalue references and move semantics (primarily) by Dave Abrahams. It also considers return value optimizations, exception safety and in what state an object should be left in after its resources have been “stolen”. Here it is.
[last update: 2009-05-27]
Occasionally I happen to see a piece of C++ code from an eager author that tries to utilize rvalue references but doesn’t understand them and gets it wrong. I remember that I had trouble fully grasping the rvalue reference concept. But now, it seems like that was way in the past. I can hardly remember why it was so difficult to understand rvalue references.
It may have something to do with the sources people use to learn about this feature. For example I wouldn’t consider the C++0x wikipedia article to be a good introduction for rvalue references. It seems too short for that. But this wikipedia article is a nice overview of what kind of features can be expected.
Let me just mention the real sources worth reading:
- N1377: Move Proposal (2002)
- N1385: Perfect Forwarding Problem (2002)
- N2027: A Brief Introduction to Rvalue References (2006)
- N2812: A Safety Problem with Rvalue References (2008)
- N2844: Fixing a Safety Problem with Rvalue References (Revision 1, 2009)
- Rvalue References 101by Dave Abrahams
I would recommend N2027 as an introduction and Dave Abraham’s article for more practical details including exception safety and return value optimizations.
“function” is a useful library feature of Boost, TR1, and the upcoming C++ standard library. It’s basically a class template mimicing the behaviour of a function pointer. But it’s much more flexible because it applies to any “callable object” — function pointers and objects with overloaded function call operator. Also, the signatures don’t have to match perfectly as long as the parameters and return values are implicitly convertible. I’m going to show you how this library feature can be implemented with C++0x features — including variadic templates and rvalue references. But before going into any details let me give an example of how code that uses this “function” library might look like. In case you’re already familiar with this library feature you can skip the following piece of code and paragraph of text.
Last update: 2009-02-1
Concepts and rvalue references are two new features that are going to be included in the next official version of the C++ language with high probability. I’m not going to mention the basics behind those two features because it has been done before in other articles. Try this google video or this article for an introduction to concepts by Doug Gregor and this article for an introduction to rvalue references by Howard Hinnant et al.
I already wrote a post on concepts and rvalue references here which discussed some problems when concepts are combined with rvalue references. It turned out that David Abrahams and Doug Gregor were also aware of the first issue I addressed. They published an article one day before I wrote about it. In this post I’m going to address other arguments (consistency and intuitivity) for why I think their proposed resolution is a good idea and what other language modification can increase consistency. Before I go into the last bit I will shed some light on what function requirements actually mean and how they can be satisfied.
Update 2009-02-1:[ It turns out you can't exprapolate the meaning of function requirements from the concept_map checking rules because they don't cover all use cases in constrained contexts (see hole in concepts system). So, keep this in mind while reading the rest of this article.]
Last update: 2008-12-08
I noticed some unfortunate gotchas when using two of the upcoming C++ features together: Rvalue references and concepts/templates. One of these problems can actually be seen in the currend draft of the future C++ specification. As far as I can tell, it looks like a subtle but nasty bug. Check out chapter 23.2.6 [vector, page 826]:
If you havn’t heard about scope guards you might want to have a look at this ddj article. The idea of scope guards is to provide a convenient tool for resource management when exception safety is an issue. Of course you can always try to write little special-purpose classes and create instances on the stack so that some specific (“cleanup”) action is performed in the destructor. Sometimes it’s just tedious to have to write those special classes and you wish for a tool that lets you get away without them. That’s a situation where the scope guard idiom could help you.