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 を書いています。これは、すべての要素を設定する 1 つだけで、多くのことを行うためです。この種の foo の再帰的な定義を維持したいと考えていましたが、boost::tuples から std::tuples に切り替えました。