【发布时间】:2015-02-15 06:23:35
【问题描述】:
我不知道如何使用从迭代器中检索到的可变数量的参数来调用函数。我有一个使用std::plus 的简单示例来说明问题:
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/apply.hpp>
#include <iostream>
#include <array>
namespace mpl = boost::mpl;
int main() {
using OP = std::plus<mpl::_>;
std::array<int, 2> args = {1, 2};
// This works (passing each arg separately)
auto n1 = mpl::apply<OP, int>::type()(args[0], args[1]);
std::cout << "plus (1,2) = " << n1 << std::endl;
// what I want is more like:
// auto n2 = mpl::apply<OP, int>::type()(args);
// or
// auto n3 = mpl::apply<OP, int>::type()(args.begin(), args.end());
}
我不知道如何跨越这个编译时/运行时界限。感谢您的任何指点!
【问题讨论】:
-
首先,大小必须是编译时间常数;然后使用
make_index_sequence得到一个索引包,最后做一个包扩展得到一个元素列表。 -
@piotr:您的解决方案正是我想要的。我已将其复制到答案部分。随意复制我的答案,我会接受!