可变参数lambda捕获的解决方法

时间:2013-11-15 17:02:25

标签: c++ c++11 lambda variadic-templates

我在这里看到了一些与gcc错误有关的问题,它在lambda中捕获了可变参数。例如,请参阅:Does lambda capture support variadic template argumentsCompiler bug, or non standard code? - Variadic template capture in lambda。我有以下设计的例子,我正在尝试做什么

#include <iostream>
#include <functional>

class TestVariadicLambda {
public:

    template<typename... Args>
    std::function<void()> getFunc(Args... args) {
        return [=]{ printArgs(args...); };
    }

    template<typename T, typename... Args>
    void printArgs(T value, Args... args) {
        std::cout << value << ", ";
        printArgs(args...);
    }

    void printArgs() {std::cout << "\n";}
};

在gcc 4.8.2中,我收到以下错误:

../src/TestVariadicLambda.h: In lambda function:
../src/TestVariadicLambda.h:9:25: error: parameter packs not expanded with ‘...’:
   return [=]{ printArgs(args...); };
                         ^
../src/TestVariadicLambda.h:9:25: note:         ‘args’
../src/TestVariadicLambda.h:9:29: error: expansion pattern ‘args’ contains no argument packs
   return [=]{ printArgs(args...); };
                         ^

我的问题是我如何解决这个问题,因为它不适用于gcc4.8

2 个答案:

答案 0 :(得分:1)

以下内容可行。

#include <iostream>
#include <functional>
#include <vector>

std::string to_string(const char* s)
{
    return s;
}

class Test
{
    private:
        void print() {}

    public:
        template<typename T, typename... Args>
        void print(T value, Args... args)
        {
            std::cout << value << "\n";
            print(args...);
        }

        template<typename... Args>
        std::function<void()> getFunc(Args... args)
        {
            using namespace std;
            std::vector<std::string> ArgumentList;
            std::initializer_list<int> {(ArgumentList.push_back(to_string(args)), 0)...};
            return [=] {for (const auto &t : ArgumentList){print(t);}};
        }
};

int main()
{
    Test().getFunc("Hey", 1, 2, 3.0f)();
}

答案 1 :(得分:1)

这是一个至少在GCC 4.8.0上静态工作的解决方法(也应该适用于VisualStudio和clang)

(代码主要来自this answer,我所做的是将参数包装在元组中然后使用元组作为旁路)

#include <iostream>
#include <functional>
#include <tuple>
using namespace std;

// ------------- UTILITY---------------
template<int...> struct index_tuple{}; 

template<int I, typename IndexTuple, typename... Types> 
struct make_indexes_impl; 

template<int I, int... Indexes, typename T, typename ... Types> 
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...> 
{ 
    typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type; 
}; 

template<int I, int... Indexes> 
struct make_indexes_impl<I, index_tuple<Indexes...> > 
{ 
    typedef index_tuple<Indexes...> type; 
}; 

template<typename ... Types> 
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...> 
{};


template<class Ret, class... Args, int... Indexes > 
Ret apply_helper( Ret (*pf)(Args...), index_tuple< Indexes... >, tuple<Args...>&& tup) 
{ 
    return pf( forward<Args>( get<Indexes>(tup))... ); 
} 

template<class Ret, class ... Args> 
Ret apply(Ret (*pf)(Args...), const tuple<Args...>&  tup)
{
    return apply_helper(pf, typename make_indexes<Args...>::type(), tuple<Args...>(tup));
}

template<class Ret, class ... Args> 
Ret apply(Ret (*pf)(Args...), tuple<Args...>&&  tup)
{
    return apply_helper(pf, typename make_indexes<Args...>::type(), forward<tuple<Args...>>(tup));
}



/// ------------------- REAL CODE --------

void printArgs() {std::cout << "\n";}

template<typename T, typename... Args>
void printArgs(T value, Args... args) {
    std::cout << value << ", ";
    printArgs(args...);
}

template<typename... Args>
std::function<void()> getFunc(Args... args) {
    std::tuple<Args...> tup(args...);
    return [=]{ apply<void, Args...>(printArgs, tup); };
}


int main(){
    auto f = getFunc<int,float,const char*>(4,5.4f,"hello");
    f();
    return 0;
}

为什么不使用std :: bind?基本上std :: bind可以为你的欲望参数创建一个lambda(只是语法糖,为你选择最可行的方法:))