vendredi 1 novembre 2019

Is it ok to return default argument's value by const reference?

Is it ok to return default argument's value by const reference like in the examples below:

https://coliru.stacked-crooked.com/a/ff76e060a007723b

#include <string>

const std::string& foo(const std::string& s = std::string(""))
{
    return s;
}

int main()
{
    const std::string& s1 = foo();
    std::string s2 = foo();

    const std::string& s3 = foo("s");
    std::string s4 = foo("s");
}

Aucun commentaire:

Enregistrer un commentaire