In C ++ 11, is there still a need to pass a reference to an object that will receive the output of the function?

Before C ++ 11, if I had a function working on large objects, my instinct would be to write functions with such a prototype.

void f(A &return_value, A const &parameter_value);

(Here, return_value is just an empty object that will receive the output of the function. A is just some class that is large and expensive to copy.)

In C ++ 11, using move semantics, the default recommendation (as I understand it) is simpler:

A f(A const &parameter_value);

Is there still a need to do this in the old way, passing in an object to store the return value?

+5
source share
3 answers

, A . , A . , "out":

A - - vector string, , "out" ( ), f, , . , :

void get_info(std::string&);
bool process_info(const std::string&);

void
foo()
{
    std::string info;
    for (bool not_done = true; not_done;)
    {
        info.clear();
        get_info(info);
        not_done = process_info(info);
    }
}

std::string get_info();
bool process_info(const std::string&);

void
foo()
{
    for (bool not_done = true; not_done;)
    {
        std::string info = get_info();
        not_done = process_info(info);
    }
}

string , . string ( ).

, std::string . , .

+7

, , . :

struct A {
    std::array<double,100000> m_data;
};

, - , , , out.

+3

, return-value-optimization, f RVO, ?

, , , . , , , . , A.

, . A , (, ..), , . , ; , , , , . , - .

+3

All Articles