我受到其他答案之一的启发,该答案建议制作一个将正确数量的参数传递给函子的包装器。我发现这个解决方案非常好,并认为我会尝试制作一个通用的模板包装器,其中参数的数量不是硬编码的。这是我想出的:
#include <string>
#include <functional>
#include <iostream>
struct WrapperHelp
{
template
< typename L
, typename Tuple
, std::size_t... Is
, typename... Ts
>
static auto apply(L&& l, Tuple t, std::index_sequence<Is...>, Ts&&... ts)
-> decltype(l(std::get<Is>(t)...))
{
return l(std::get<Is>(t)...);
}
template
< typename L
, typename Tuple
, std::size_t... Is
, typename T1
, typename... Ts
>
static auto apply(L&& l, Tuple t, std::index_sequence<Is...>, T1&& t1, Ts&&... ts)
-> decltype(WrapperHelp::apply(std::forward<L>(l), std::forward_as_tuple(std::get<Is>(t)..., t1), std::make_index_sequence<sizeof...(Is) +1 >(), ts...))
{
return WrapperHelp::apply(std::forward<L>(l), std::forward_as_tuple(std::get<Is>(t)..., t1), std::make_index_sequence<sizeof...(Is) + 1>(), ts...);
}
};
template<typename L>
struct OptionalWrapper {
public:
OptionalWrapper(L l) : lambda{std::move(l)} {}
template<typename... Ts>
void operator()(Ts&&... ts) const
{
WrapperHelp::apply(lambda, std::tuple<>(), std::index_sequence<>(), std::forward<Ts>(ts)...);
}
private:
L lambda;
};
template<typename L>
auto makeOptionalWrapper(L l) { return OptionalWrapper<L>{std::move(l)}; }
template<class F>
void loadData(OptionalWrapper<F>&& callback)
{
std::string body = "body";
std::string subject = "subject";
std::string header = "header";
double lol = 2.0;
callback(body, subject, header, lol);
}
template<typename L>
void loadData(L callback)
{
loadData(makeOptionalWrapper(std::move(callback)));
}
int main() {
//apply(std::tuple<double>(2), std::tuple<double>(2));
loadData([](auto&& body) {
std::cout << body << std::endl;
});
loadData([](auto&& body, auto&& subject) {
std::cout << body << " " << subject << std::endl;
});
loadData([](auto&& body, auto&& subject, auto&& header) {
std::cout << body << " " << subject << " " << header << std::endl;
});
loadData([](auto&& body, auto&& subject, auto&& header, auto&& lol) {
std::cout << body << " " << subject << " " << header << " " << lol << std::endl;
});
return 0;
}
这应该适用于任何函数,具有任意数量的“可选”参数,以及任何类型的参数。这不是最漂亮的代码,但我希望这个想法很清楚并且可以使用:)
活生生的例子