I want to write a lambda that takes an arbitrary number of arguments with a universal reference and completely ignores them. An obvious method would be to use the syntax for a variable universal parameter package and omit the parameter name:
auto my_lambda = [](auto&&...) { return 42; };
This works fine (with gcc 4.9.2) until I try to pass in an object not subject to trivial copying :
struct S { S() {} S(S const&) {} }; my_lambda("meow", 42, S{}); ^ error: cannot pass objects of non-trivially-copyable type 'struct S' through '...'
What's happening? Is my code badly formed, or is this a bug in gcc?
Anyway, what's the best workaround? I found that parameter naming works, but then I came across a warning about an unused parameter:
auto my_lambda = [](auto&&... unused) { return 42; }; ^ error: unused parameter 'unused#0' [-Werror=unused-parameter] ^ error: unused parameter 'unused#1' [-Werror=unused-parameter] ^ error: unused parameter 'unused#2' [-Werror=unused-parameter]
How do you suppress a warning about an unused parameter in the template parameters package?
c ++ lambda c ++ 14 variadic
ecatmur
source share