c++ - Is it possible to expand a parameter pack with a lambda function? -
i think question corner case, of how far can go parameter pack expansion , it's little hard explain, want. idea easiest express little code, lets have @ following lines:
// <main.cpp> #include <tuple> #include <iostream> // function supposed return std::tuple of function objects // each template argument type, there should 1 such function object, // takes value of type , returns half. template <class... arg_t> auto make_divide_by_two_lambda_tuple() { return std::make_tuple( [](const arg_t& elem) -> arg_t { return elem/2; }... ); } int main() { // lets initialise tuple our function auto value = make_divide_by_two_lambda_tuple<int, float>(); /* * if workes planned, tuple has structure * value<0> := [](const int&){ ... } * value<1> := [](const float&){ ... } */ // , able use our functions in expected way std::cout << "5/2 (int version) = " << std::get<0>(value)(5) << std::endl; std::cout << "5/2 (float version) = " << std::get<1>(value)(5) << std::endl; return 0; }
for clang(4.0.1), example compiles , run expect do:
$ clang++ -std=c++14 ../main.cpp $ ./a.out 5/2 (int version) = 2 5/2 (float version) = 2.5
for g++(7.1.1), example fail compile:
$ g++ -std=c++14 ../main.cpp ../main.cpp: in function ‘auto make_divide_by_two_lambda_tuple()’: ../main.cpp:7:28: error: parameter packs not expanded ‘...’: [](const arg_t& elem) -> arg_t ^~~~~ ../main.cpp:7:28: note: ‘arg_t’ ../main.cpp:7:28: error: parameter packs not expanded ‘...’: ../main.cpp:7:28: note: ‘arg_t’ ../main.cpp:10:4: error: expansion pattern ‘<lambda>’ contains no argument packs }... ^~~ ../main.cpp: in function ‘int main()’: ../main.cpp:15:10: error: ‘void value’ has incomplete type auto value = make_divide_by_two_lambda_tuple<int, float>();
so seems, gcc willing use parameter pack inside argument list of lambda expression. using whole lambda stretching far gcc accept.
is use of lambda expressions undefined behaviour, or 1 compiler (hopefully gcc) wrong?
ps.: checked docs, explains, can use parameter pack expansion, , not statments can expand.
Comments
Post a Comment