Search code examples
c++c++11virtualmove-semanticscopy-elision

In c++11, can a virtual function return a large value efficiently with move semantics?


Normally, this would be optimised to not involve copying the large value (since a std::vector has move semantics enabled):

std::vector<int> makeABigThing(){
    std::vector<int> large_thing(1000, 0);
    return large_thing;
}

Can this also be optimised in the same way if the function is a virtual method:

struct Foo{
    virtual std::vector<int> makeABigThing(){
        std::vector<int> large_thing(1000, 0);
        return large_thing;
    }
};

i.e., do move semantics work with even when the called function is selected at runtime?


Solution

  • Whether the function is static or dynamically resolved does not affect the possibility of moving the result.