开发者

STL swap on return?

sorry for such a long question but I try to be as clear as possi开发者_如何学Goble. This somehow follows my previous question about strings in C++. I'm trying to figure out how I could return std::string from a function without redundant memory allocations, without relying on NRVO. The reasons why I don't want to rely on NRVO are:

  • it is not supported by the compiler we currently use
  • even when it is supported it might not always be enabled in Debug mode
  • it might fail in some cases (example)

Please note that I need a C++03 compatible solution (no C++0x rvalue references thus, unfortunately...)

The simplest way do this is pass-by-reference and do std::swap, like this

void test(std::string& res)
{
    std::string s;
    //...
    res.swap(s);
}

But it is more natural and often more convenient to return by value than pass by reference, so what I want to achieve is this:

std::string test()
{
    std::string s;
    //...
    return SOMETHING(s);
}

Ideally it would just do a swap with the "return value", but I don't see how to do this in C++. There is auto_ptr already which does move instead of copy, and I could actually use auto_ptr<string>, but I'd like to avoid dynamically allocating the string object itself.

My idea is to somehow "tag" a string object that it is being returned from a function to permit moving its data when a copy constructor is called on return. So I ended up with this code, which does exactly what I want:

struct Str
{
    struct Moveable
    {
        Str & ref;
        explicit Moveable(Str & other): ref(other) {}
    };

    Str() {}
    Str(const std::string& other) : data(other) {} // copy
    Str(Moveable& other) { data.swap(other.ref.data); } // move

    Moveable Move()
    {
        return Moveable(*this);
    }

    std::string data;
};

Str test()
{
    Str s;
    //...
    return s.Move(); // no allocation, even without NRVO
}

So... Does all this make sense, or there are some serious issues that I'm missing? (I'm not sure if there is no lifetime issue for example). Maybe you have already seen such idea in a library (book, article...), and could give me a reference to it?

EDIT: As @rstevens noticed, this code is MSVC-specific and won't compile under g++ which does not like the non-const temporary. This is an issue, but let's just assume this implementation is MSVC-specific.


The implementation of boost uses move semantics emulation internally for libraries like Boost.Thread. You may want to look at the implementation and do something similar.

Edit: there is actually an active development of a library Boost.Move, so you can already start using it.


Did you check this code on g++?

Since you call Str(Movable &) with a temporary object (the one returned by s.Move())!

This is not standard's compliant and not supported by g++. It is supported by MSVC! (MS calls this a feature...).


Have you actually determined that returning by value is a performance problem in your application? That seems like the simplest/easiest way to go, and when you upgrade to a more modern compiler you can use rvalue references.

I can't answer the question regarding the destruction order of s vs the Movable's reference. You could, for your compiler, put code in the various constructors and destructors to see what the order is. Even if it looks ok I'd still consider using one of the normal patterns you outlined though just to prevent reader confusion and possibly breaking on an alternate compiler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜