Pizer’s Weblog

programming, DSP, math

Posts Tagged ‘rvalues

C++: Exploiting Copy Elisions

with 8 comments

This article is about writing efficient code and giving the compiler opportunities for optimizations with respect to copying / “moving” objects in good old C++98. I use a simple function as an example but it should extend to other cases as well. The technique that is described here (“swaptimization”) is not entirely new and I can’t take credit for it. But it is probably little-known which is a good reason to write about it. 🙂

Read the rest of this entry »


Written by pizer

June 25, 2009 at 4:57 pm

Surprizing C++ reference-binding rules

with one comment

Isn’t it amazing? You see a very simple piece of code that looks perfectly fine and wonder why it is supposed to be ill-formed:

  struct foo {
    foo(const foo& rhs);

  void func1(const foo&);

  void func2() {
    foo o;
    func1( o ); // ok
    func1( foo() );  // ill-formed, foo's copy-ctor is private

Although an rvalue is allowed to bind to a reference-to-const, the C++ standard allows a compiler to copy an rvalue to another temporary rvalue before binding it to a reference-to-const. Obviously, you need to have a public copy constructor for that.

I really fail to see the merit in this rule. It seems that every available C++ compiler does not create an extra temporary object here and some of these compilers don’t even bother to report an error in this situation (g++ version 3.4.5 accepts the above code).

Fortunately, the upcoming version of the C++ standard removes the CopyConstructible requirement for reference binding. It makes the code above well-formed.

Source: this usenet thread

– P

Written by pizer

March 25, 2009 at 1:56 pm