如何将lambda表达式作为参数传递给可变模板类中的mermber函数

我想设计一个带有元组的模板类,提供该类的一个apply()成员函数,它接受一个返回元组的lambda函数。lambda的返回元组的类型应该与模板类的类型列表兼容。但是它不能编译。

main.cpp: In function ‘int main()’:
warning: lambda templates are only available with -std=c++14 or -std=gnu++14
     auto getTuple = []<typename ... Param, typename ... Types>(const Param & ..
                   ^
main.cpp: In lambda function:
main.cpp:75:32: error: parameter packs not expanded with ‘...’:
         std::tuple<Types>  items = std::tuple<Types>(p...);
                            ^
main.cpp:75:32: note:         ‘Types’
main.cpp:19:43: error: invalid use of ‘auto’
     std::tuple<Types...>  t = getTuple(param...);    

#include <iostream>
#include <tuple>
#include <string>
#include <type_traits>
#include <array>
template <typename... Types>
class Data
{
public:
    template<typename ... Param, typename Functor>
    std::tuple<Types...> apply(Functor getTuple, const Param& ... param)
    {
        std::tuple<Types...>  t = getTuple(param...);
        return t;
    }
};

int main()
{
    Data<std::string, int,double> data1;
    auto getTuple = []<typename ... Param, typename ... Types>(const Param & ... p)
    {
        std::tuple<Types>  items = std::tuple<Types>(p...);
        return items;
    };
    const auto t = data1.apply(getTuple , "hello", 3,1.9);

    return 0;
}

转载请注明出处:http://www.yaohuano3.com/article/20230526/1935745.html