namespace easy_random {
template<typename First_T>
auto make_tuple_of_rands(First_T first) {
return std::make_tuple(first());
}
template<typename First_T, typename... Rest_T>
auto make_tuple_of_rands(First_T first, Rest_T... rest) {
return std::tuple_cat(make_tuple_of_rands(first),make_tuple_of_rands(rest...));
}
template<typename First_T>
auto make_tuple_of_n_rands(std::size_t n, First_T first) {
return std::make_tuple(first(n));
}
template<typename First_T, typename... Rest_T>
auto make_tuple_of_n_rands(std::size_t n, First_T first, Rest_T... rest) {
return std::tuple_cat(make_tuple_of_n_rands(n, first), make_tuple_of_n_rands(n, rest...));
}
template<typename... RandStreamTypes>
class multi_rand_stream {
std::tuple<RandStreamTypes...> streams;
public:
explicit multi_rand_stream(RandStreamTypes... args);
explicit multi_rand_stream(std::tuple<RandStreamTypes...> &args);
auto operator()();
auto operator()(std::size_t n);
};
template<typename... RandStreamTypes>
multi_rand_stream<RandStreamTypes...>::multi_rand_stream(RandStreamTypes... args) : streams(args...) {}
template<typename... RandStreamTypes>
multi_rand_stream<RandStreamTypes...>::multi_rand_stream(std::tuple<RandStreamTypes...> &args) : streams(args) {}
template<typename... RandStreamTypes>
auto multi_rand_stream<RandStreamTypes...>::operator()() {
return std::apply(make_tuple_of_rands, streams);
}
template<typename... RandStreamTypes>
auto multi_rand_stream<RandStreamTypes...>::operator()(std::size_t n) {
return std::apply(make_tuple_of_n_rands, std::tuple_cat(std::make_tuple(n), streams));
}
}
我希望能够通过将tup的成员用作函子来返回由返回值组成的元组。
我目前对如何处理方法一无所知。我曾尝试将apply和recursion与tuple_cat结合使用,但是在推断函数的模板类型时遇到了麻烦。
编辑:包括我要实现的完整类,也许这将暴露我的错误。
答案 0 :(得分:1)
回答以上问题:我只需要使用std::apply()
将元组展开为可变的lambda。然后,您可以在其中以参数包的形式接收元组元素,然后可以将其扩展到一个初始化器列表,该列表可通过调用每个元素来创建新的元组:
template<typename... Ts>
class multi_functor
{
std::tuple<Ts...> tup;
public:
auto operator()()
{
return std::apply([](auto&&... args)
{
return std::tuple { args()... };
}, tup);
}
};