我想设计一个带有元组的模板类,提供类的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;
}
答案 0 :(得分:2)
您无法为lambda提供模板:您只需使用auto
代替:
auto getTuple = [](const auto& ... p)
{
auto items = std::make_tuple(p...);
return items;
};