10

コンパイル時にタプル要素を置き換える方法はありますか?

例えば、

using a_t = std::tuple<std::string,unsigned>;  // start with some n-tuple
using b_t = element_replace<a_t,1,double>;     // std::tuple<std::string,double>
using c_t = element_replace<b_t,0,char>;       // std::tuple<char,double>
4

3 に答える 3

19

これを使用できます:

// the usual helpers (BTW: I wish these would be standardized!!)
template< std::size_t... Ns >
struct indices
{
    typedef indices< Ns..., sizeof...( Ns ) > next;
};

template< std::size_t N >
struct make_indices
{
    typedef typename make_indices< N - 1 >::type::next type;
};

template<>
struct make_indices< 0 >
{
    typedef indices<> type;
};

// and now we use them
template< typename Tuple, std::size_t N, typename T,
          typename Indices = typename make_indices< std::tuple_size< Tuple >::value >::type >
struct element_replace;

template< typename... Ts, std::size_t N, typename T, std::size_t... Ns >
struct element_replace< std::tuple< Ts... >, N, T, indices< Ns... > >
{
    typedef std::tuple< typename std::conditional< Ns == N, T, Ts >::type... > type;
};

そして、次のように使用します。

using a_t = std::tuple<std::string,unsigned>;     // start with some n-tuple
using b_t = element_replace<a_t,1,double>::type;  // std::tuple<std::string,double>
using c_t = element_replace<b_t,0,char>::type;    // std::tuple<char,double>
于 2013-03-14T14:27:42.700 に答える
5

ブーストMPLtransformまたはreplaceアルゴ を見てください http://www.boost.org/doc/libs/1_40_0/libs/mpl/doc/refmanual/transformation-algorithms.html

于 2013-03-14T13:47:46.070 に答える
0

std::tuple_elementを使用して、タプル型の要素の型にアクセスできます。これにより、実際にはタプル要素の型を置き換えることはできませんが、他のタプル型で要素型として使用される型に関してタプル型を定義できます。

于 2013-03-14T13:53:51.320 に答える