sytpb 发表于 2016-06-06 15:52

C++11 auto decltype 一个疑问

本帖最后由 sytpb 于 2016-06-06 16:01 编辑

请教,代码如下

template<typename R>
auto GetFun2(int nType,R& r)->function<decltype(r)(string)>
{
   if(nType == 1)
         return [](string s){return stoi(s);};

}

void callGetFun2()
{
   int r =100;
   GetFun2(1,r);
}



callGetFun2()调用出错

ship.cpp:186:12: error: no viable conversion from '<lambda at ship.cpp:186:12>' to 'function<decltype(r) (string)>'
                  return [](string s){return stoi(s);};

panyi423 发表于 2016-06-06 17:45

decltype(r) 返回的类型为 int&
[](string s) {return stoi(s);}; 返回的类型为int

sytpb 发表于 2016-06-07 07:16

回复 2# panyi423


    thanks   恍然大悟。
页: [1]
查看完整版本: C++11 auto decltype 一个疑问