2013-05-13 130 views

回答

7

只需使用省略號。每款C++ 11標準的5.1.2/23:

A capture followed by an ellipsis is a pack expansion (14.5.3). [ Example:

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

end example ]

注:有趣的是,GCC拒絕編譯這個(見live example):

template <class T, class... A> 
void foo(T&& func, A&&... args) { 
    std::function<void()> task([func, args...]() { 
     //... 
    }); 
} 

但考慮到上述來自標準的例子,這絕對是一個編譯器問題。

+0

令人驚訝的是,你是否記住了整個標準或只是做一個快速的樣子? – maverik 2013-05-13 17:25:01

+0

@maverik:當然我做了一個快速的檢查;) – 2013-05-13 17:25:56

+0

看起來像這個海灣合作委員會的錯誤:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933(你的報價是一個相當晚的除標準) – 2013-05-13 17:53:55

3

當您在拍攝使用args,你需要省略號:

template <class T, class... A> 
void tpool::enqueue(T&& func, A&&... args) { 
    std::function<void()> task([func, args...]() { 
     //... 
    }); 
} 
+0

我試過你的代碼,但不能再次編譯。 'error:expected','before'...'token''錯誤:'...'token'之前的預期標識符'錯誤:參數包沒有用'...'擴展' – Mpac 2013-05-13 17:35:28

+0

@ MarcoPacini您使用的是GCC嗎?在這種情況下,編譯器中似乎存在一個錯誤(例如[Andy Prowl回答](http://stackoverflow.com/a/16527660/440558))。 – 2013-05-13 17:38:34

+0

是的,我使用安裝了macport的GCC 4.7.2。 – Mpac 2013-05-13 17:42:18