5

autoauto&const autoおよびconst auto&(たとえば「for each」ループ内)の違いは知っていますが、1 つ驚いたことは次のとおりです。

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
}

xでは、式auto x = fun();の の型が の戻り値の型と同じ型にならない場合を誰か教えてもらえfun()ますか?

4

2 に答える 2

18

のルールautoは、テンプレート タイプの推定と同じです。

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&&

一般に、コピーが必要な場合は を使用autoし、参照が必要な場合は を使用しますauto&&auto&&レフェレンの一貫性を維持し、一時的にバインドすることもできます (寿命を延ばします)。

于 2012-08-21T10:11:34.227 に答える
1

g++-4.8 では、関数の戻り値の型の自動推定が強化されています。

2012-03-21 ジェイソン・メリル

Implement return type deduction for normal functions with -std=c++1y.

-std=c++1y または -std=gnu++1y フラグが必要です。

これは機能します: auto sluggo() { return 42; }

int
main()
{
    auto s1 = sluggo();
    s1 += 7;
}

OPの質問エラーは+="YYY"、期待どおりにのみ発生します。auto を使用して cf を宣言することもできます。

#include <string>

std::string bla;

const auto&
cf()
{
    return bla;
}


int
main()
{
    auto s1 = cf();
    const std::string& s2 = cf();
    s1 += "XXX"; // not an error
    s2 += "YYY"; // error as expected
}

でまだエラーが発生してい+="YYY"ます。

于 2012-08-22T20:09:23.463 に答える