你的问题似乎有两个方面.首先,您的问题的标题询问是否可以将数组的元素扩展为函数的参数.确实有可能,因为std::array
是Foldable
.它足以使用hana::unpack
:
#include#include #include namespace hana = boost::hana; struct myfunction { template void operator()(T ...i) const { // whatever } }; int main() { std::array xs = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; hana::unpack(xs, myfunction{}); }
其次,你问是否有可能做类似的事情
std::string s; std::arrayxs = {1, 3, 5, ...}; hana::int_c<10>.times([&](int i){ s += std::to_string(xs[i]) + ","; });
答案是使用hana::int_c<10>.times.with_index
:
hana::int_c<10>.times.with_index([&](int i) { s += std::to_string(xs[i]) + ","; });
同样,您也可以使用hana::for_each
:
hana::for_each(xs, [&](int x) { s += std::to_string(x) + ","; });