2010-07-31 110 views
10

做什麼用的g ++做這個正確的方法:我認爲你需要擴大在捕獲列表包a爲好,這樣的拉姆達可變參數模板表達式

template < typename F > 
void g (F f); 

template < typename ... A > 
void h (A ... a); 

template < typename ... A > 
void f (A ... a) { 
    g ([&a]() { h (a...); }); // g++-4.6: error: parameter packs not expanded with »...« 
} 

回答

16

template < typename ... A > 
void f (A ... a) { 
    g ([&, a...]() { h (a...); }); 
} 

以下是來自C++ 0x Final Committee草案第5.1.2.23節的相關文本:

捕獲後跟省略號是 包擴展(14.5.3)。 [實施例:

template<class... Args> void f(Args... args) { 
    auto lm = [&, args...] { return g(args...); }; lm(); 
} 

- 端示例]

+0

謝謝。 g ++ - 4.6不接受草案中的這種語法:test01.cc:2:23:Fehler:expected»,«before»...«token – Thomas 2010-07-31 19:58:09

+3

我通過創建一個元組來解決它,並將它傳遞給lambda函數並在那裏解包。 – Thomas 2010-07-31 21:08:36

+3

它看起來像g ++ - 4.8仍然不接受這種語法,但ICC 13.0.1會:http://bit.ly/14auYGy – SCFrench 2013-05-10 16:56:27

0
#include <functional> 
template < typename ... A > 
void f (A ... a) { 
    g (std::bind(h, a...)); 
}