Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 auto and function return types

I know of the difference between auto, auto&, const auto and const auto& (for example in a "for each" loop), but one thing that surprised me is:

std::string bla;
const std::string& cf()
{
    return bla;
}


int main (int argc, char *argv[])
{
    auto s1=cf();
    const std::string& s2=cf();
    s1+="XXX"; // not an error
    s2+="YYY"; //error as expected
}

So can somebody tell me when the type of x in the expression auto x = fun(); won't be the same type as the type of the return value of the fun()?

like image 610
NoSenseEtAl Avatar asked Aug 21 '12 10:08

NoSenseEtAl


1 Answers

The rules for auto are the same as for template type deduction:

template <typename T>
void f(T t); // same as auto
template <typename T>
void g(T& t); // same as auto&
template <typename T>
void h(T&& t); // same as auto&&

std::string sv;
std::string& sl = sv;
std::string const& scl = sv;

f(sv); // deduces T=std::string
f(sl); // deduces T=std::string
f(scl); // deduces T=std::string
f(std::string()); // deduces T=std::string
f(std::move(sv)); // deduces T=std::string

g(sv); // deduces T=std::string, T& becomes std::string&
g(sl); // deduces T=std::string, T& becomes std::string&
g(scl); // deduces T=std::string const, T& becomes std::string const&
g(std::string()); // does not compile
g(std::move(sv)); // does not compile

h(sv); // deduces std::string&, T&& becomes std::string&
h(sl); // deduces std::string&, T&& becomes std::string&
h(scl); // deduces std::string const&, T&& becomes std::string const&
h(std::string()); // deduces std::string, T&& becomes std::string&&
h(std::move(sv)); // deduces std::string, T&& becomes std::string&&

In general, if you want a copy, you use auto, and if you want a reference you use auto&&. auto&& preserves the constness of the referene and can also bind to temporaries (extending their lifetime).

like image 134
R. Martinho Fernandes Avatar answered Sep 27 '22 22:09

R. Martinho Fernandes