![](/img/trans.png)
[英]Template function causes a compiler error when used with local lambda
[英]Inline lambda expression causes compiler error
template <typename T, typename Y, typename... Args>
class Bar
{
T& t;
public:
Bar(T& t) : t(t) { }
};
template <typename T, typename... Args>
void Foo(T &function) { new Bar<T, void, Args...>(function); }
int main()
{
auto foo = [] { };
Foo(foo); // ok
Foo([] { }); // fails (tested on GCC 4.5.3)
}
為什么僅當lambda表達式作為Foo的參數內聯寫入時才失敗?
template <typename T, typename... Args>
void Foo(T &function) { new Bar<T, void, Args...>(function); }
Foo([] { }); // fails (tested on GCC 4.5.3)
Lambda是臨時的。 不要嘗試將臨時綁定到參考。 使用值,或const-reference或rvalue-reference。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.