相关疑难解决方法(0)

C++ 11自动和函数返回类型

我知道之间的区别的auto,auto&,const autoconst auto&(例如在"每个"循环),但有一两件事令我惊讶的是:

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
}
Run Code Online (Sandbox Code Playgroud)

那么有人可以告诉我何时x表达式auto x = fun();中的类型与返回值的类型不是同一类型fun()

c++ type-inference c++11

5
推荐指数
1
解决办法
3678
查看次数

标签 统计

c++ ×1

c++11 ×1

type-inference ×1