我有以下簡單的場景:語法錯誤與lambda表達式模板類
template< typename T>
struct A
{
A() : action_([&](const T& t) { })
{}
private:
boost::function< void(const T&)> action_;
};
當使用Visual C++ 2010編譯,它給了我在建設action_的語法錯誤:
1>test.cpp(16): error C2059: syntax error : ')'
1> test.cpp(23) : see reference to class template instantiation A<T>' being compiled
奇怪的是,沒有模板參數的相同示例編譯得很好:
struct A
{
A() : action_([&](const int& t) { })
{}
private:
boost::function< void(const int&)> action_;
};
我知道,一個解決辦法的問題是移動action_初始化在構造函數體,而不是初始化列表,如下面的代碼:
template< typename T>
struct A
{
A()
{
action_ = [&](const T& t) { };
}
private:
boost::function< void(const T&)> action_;
};
...但我想避免這樣的解決方法。
有沒有人遇到過這種情況?是否有任何解釋/解決這個所謂的語法錯誤?
FWIW,提供的示例(在修復缺少的'#include'後)使用G ++ 4.6.1編譯時不會發出警告。 –
它是特定於Visual C++ 2010. –