我想递归地定义一个设置 std::tuple 的所有元素的函数。如果它是一个 boost::tuple,我会简单地看这里并复制这个例子:
inline void set_to_zero(const null_type&) {};
template <class H, class T>
inline void set_to_zero(cons<H, T>& x) { x.get_head() = 0; set_to_zero(x.get_tail()); }
到目前为止,对于 std::tuple,我找不到任何几乎那么简单的东西。附近有什么吗?
更新:好的,我需要进一步充实这个问题。
template<typename T>
struct simple_setter
{
void set(T &target); // somebody else's stuff, defined elsewhere
};
template<typename Tuple> class foo;
template<>
struct foo<boost::tuple<>>
{
foo() {}
void set(boost::tuple<> &target) {}
};
template<typename E, typename... Es>
struct foo<boost::tuple<E, Es...>>
{
explicit foo(const simple_setter<E> &head_setter, const simple_setter<Es> & ... other_setters) :
_head(head_setter),
_tail(other_setters)
{}
void set(boost::tuple<E, Es...> &target) {
_head.set(target.get_head()),
_tail.set(target.get_tail())
}
// more methods to do other things
const simple_setter<E> &_head;
const foo<boost::tuple<Es ...>> _tail;
};
我正在编写包装元组的类 foo ,以便做很多事情,其中只有一个是设置所有元素。我希望保留这种对 foo 的递归定义,但要从 boost::tuples 切换到 std::tuples。