【发布时间】:2018-01-28 18:25:47
【问题描述】:
以下行在 c++ 11 中给出错误:
function<bool(string,string)> comp = [] (string& s1, string& s2) {return s1.length() > s2.length(); };
但事实并非如此:
function<bool(string,string)> comp = [] (const string& s1, const string& s2) {return s1.length() > s2.length(); };
第二次调用的参数为 const。有什么解释吗?
【问题讨论】:
-
临时对象或文字不能绑定到对非常量对象的引用。 Any good beginners book 应该告诉你的。
-
右值(比如传值的参数)不能绑定到非const左值引用,但是可以绑定到const左值引用。
-
你知道自动对 lambda 有效吗?
auto comp = ...