lundi 21 juin 2021

Which is better to get return value, by const&, by &&, or just by value?

#include <vector>

std::vector<int> f()
{
    return std::vector<int>(1024);
}

void use_1(std::vector<int> const&)
{}

void use_2(std::vector<int> const&)
{}

int main()
{
    {
        auto const& v = f(); // style 1
        use_1(v);
        use_2(v);
    }

    {
        auto&& v = f(); // style 2
        use_1(v);
        use_2(v);
    }

    {
        auto v = f(); // style 3
        use_1(v);
        use_2(v);
    }
}

Which is better? style 1, style 2, or style 3?

Aucun commentaire:

Enregistrer un commentaire