読者です 読者をやめる 読者になる 読者になる

tieで初期化

C++

tieで初期化させろ - にっき(pseudo)


いまさらながらGCC 4.5で動いたので完全なコードを:

#include <iostream>
#include <utility>
#include <boost/tuple/tuple.hpp>

#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/tuple/rem.hpp>
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/stringize.hpp>

template<size_t N, typename T>
typename boost::tuples::element<N, T>::type & get_with_tuple_ref_folding(T & tup) {
    return boost::get<N>(tup);
}
template<size_t N, typename T>
typename boost::tuples::element<N, T>::type && get_with_tuple_ref_folding(T && tup) {
    return std::move(boost::get<N>(tup));
}

#define TIE(seq, expr) TIE_I(seq, expr, __LINE__,)
#define TIE_AUTO(seq, expr) TIE_I(seq, expr, __LINE__, auto)

#define TIE_I(seq, expr, id, a) TIE_II(seq, expr, id, a)
#define TIE_II(seq, expr, id, a) auto && tie_tmp_ ## id = expr; BOOST_PP_TUPLE_ELEM(4, 3, BOOST_PP_SEQ_FOLD_LEFT(TIE_OP, (0, tie_tmp_ ## id , a,), seq))
#define TIE_OP(_, l, e) TIE_OP_I((e, BOOST_PP_TUPLE_REM(4)l))
#define TIE_OP_I(par) TIE_OP_II par
#define TIE_OP_II(e, n, name, a, s) (BOOST_PP_INC(n), name, a, s a e(get_with_tuple_ref_folding<n>(std::forward<decltype(name)>(name)));)

int main()
{
    TIE((const int x)(const int y)(const int z), boost::tuples::make_tuple(1, 2, 3));
    std::cout << x << "," << y << "," << z << std::endl;

//  TIE((const int a)(boost::tuples::ignore), boost::tuples::make_tuple(4, 5));
//  std::cout << a << std::endl;
}
1,2,3


ignoreは使えないっぽいですねぇ。