簡體   English   中英

內聯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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM