4

I tried the following code but doesn't compile.

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

2 Answers 2

7

Just use the ellipses. Per paragraph 5.1.2/23 of the C++11 Standard:

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 ]

Note: Interestingly enough, GCC is refusing to compile this (see the live example):

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

But considering the above example from the Standard, this is definitely a compiler issue.

Sign up to request clarification or add additional context in comments.

5 Comments

Amazing, do you hold in memory the whole standard or just do a quick look?
Looks like this gcc bug: gcc.gnu.org/bugzilla/show_bug.cgi?id=41933 (your quote was a rather late addition to the standard)
@MarcGlisse: Oh, interesting. Thank you for the information! Not sure if that's related, but this also fails to compile...
I assume it is the same issue (the capture is just implicit), but if you have a doubt, feel free to add a small testcase to that bug report (that might also remind people that the bug is still there). And don't hesitate to report other bugs when you find them.
@MarcGlisse fyi: both compile find under clang (llvm 3.2).
3

When you use args in the capture, you need ellipsis:

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

4 Comments

I've tried your code but doesn't compile again. I get the following error: error: expected ',' before '...' token error: expected identifier before '...' token error: parameter packs not expanded with '...'
@MarcoPacini Are you using GCC? In that case there seems to be a bug in the compiler (as in the answer by Andy Prowl).
Yes, I use GCC 4.7.2 installed with macport.
Same issue with G++ 4.8.4. The G++ ticket appears to hint that a resolution is planned for the 4.9 time-frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.