10

ばかげた理由でここには立ち入りませんが、コメントアウトされた行が機能し、その上の行が機能しないようにする必要があります。

template<uint _N, typename... _Args>
struct PartialTuple;

template<uint _N, typename _Arg, typename... _Args>
struct PartialTuple<_N, _Arg, _Args...>: PartialTuple<_N-1, _Args...> {};

template<typename _Arg, typename... _Args>
struct PartialTuple<0, _Arg, _Args...>
{
    typedef std::tuple<_Arg, _Args...> type;
};

int main()
{
    // I want this to not work...
    PartialTuple<1, std::string, std::string, int, int>::type A{"test", 5, 1};

    // I want this to work...
    //PartialTuple<1, std::string, std::string, int, int>::type B{"test", "test", 5};
}

と交換しようと_Argしまし_Args...たが、コンパイルされません (少なくとも GCC 4.6 では):

error: parameter pack argument ‘_Args ...’ must be at the end of the template argument list

頭ではなく尻尾からアイテムを引き離すにはどうすればよいですか?

4

4 に答える 4

5

解決策は次のとおりです。N後ろから切り捨てるのではなくsizeof...(Args) - N、前から切り捨てます。

#include <tuple>

/* Concatenator helper */

template <typename T, typename Tuple> struct cat;
template <typename T, typename ...Args>
struct cat<T, std::tuple<Args...>>
{
  typedef typename std::tuple<T, Args...> value;
};


/* Head-of-tuple */

template <unsigned int, typename...> struct tuple_head;

// Base case. Need to specialize twice, once for one and once for variadic types
template <typename ...Args>
struct tuple_head<0, Args...>
{
  typedef std::tuple<> value;
};
template <typename T>
struct tuple_head<0, T>
{
  typedef std::tuple<> value;
};

// Recursion step
template <unsigned int N, typename T, typename ...Args>
struct tuple_head<N, T, Args...>
{
  typedef typename cat<T, typename tuple_head<N - 1, Args...>::value>::value value;
};


/* User interface */

template <unsigned int N, typename ...Args>
struct PartialTuple
{
  typedef typename tuple_head<sizeof...(Args) - N, Args...>::value type;
};


/* Usage */

#include <string>
int main()
{
  // I want this to not work...
  //PartialTuple<1, std::string, std::string, int, int>::type A{"test", 5, 1};

  // I want this to work...
  PartialTuple<1, std::string, std::string, int, int>::type B("test", "test", 5);
  PartialTuple<0, std::string, std::string, int, int>::type C("test", "test", 5, 6);
}
于 2011-07-09T14:54:04.293 に答える
3

私のコードは、Haskell のリストのように機能するようにしました。なぜなら、TMP は C++ 内の純粋な関数型言語だからです。

add_to_packHaskell の list constructor と同等(:)です。drop_from_endは (Haskell 記法で) として実装され\x list -> take (length list - x) list、リストのtake n最初のn要素のみを取ります。

std::tupleの代わりに直接使用できると思いますがpack、タプルをテンプレートパラメーターパックホルダーとして誤用しないため、このソリューションの方が気に入りました。:)

コードは次のとおりです。

#include <tuple>
#include <type_traits> // for std::conditional


template <typename... Pack>
struct pack
{ };


template <typename, typename>
struct add_to_pack;

template <typename A, typename... R>
struct add_to_pack<A, pack<R...>>
{
  typedef pack<A, R...> type;
};


template <typename>
struct convert_to_tuple;

template <typename... A>
struct convert_to_tuple<pack<A...>>
{
  typedef std::tuple<A...> type;
};


template <int, typename...>
struct take;

template <int N>
struct take<N>
{
  typedef pack<> type;
};

template <int N, typename Head, typename... Tail>
struct take<N, Head, Tail...>
{
  typedef
    typename std::conditional<
      (N > 0),
      typename add_to_pack<
        Head,
        typename take<
          N - 1,
          Tail...
        >::type
      >::type,
      pack<>
    >::type type;
};  


template <int N, typename... A>
struct drop_from_end
{
  // Add these asserts if needed.
  //static_assert(N >= 0,
  //  "Cannot drop negative number of elements!");

  //static_assert(N <= static_cast<int>(sizeof...(A)),
  //  "Cannot drop more elements than size of pack!")

  typedef
    typename convert_to_tuple<
      typename take<
        static_cast<int>(sizeof...(A)) - N,
        A...
      >::type
    >::type type;
};


int main()
{
  drop_from_end<2, const char*, double, int, int>::type b{"pi", 3.1415};
}

そして、これが作業中のコードです: via ideone.com .


このtake構造体は、次の Haskell コードとほぼ同等です。

take n []     = []
take n (x:xs)
  | n > 0     = x : take (n - 1) xs
  | otherwise = []
于 2011-07-09T14:35:14.753 に答える
3

私は一晩中それで遊んでいて、ついに何かがうまくいくようになりました(STLに一致するようにケーシングを変更しました):

template<uint _N, typename... _All>
struct reverse_tuple_outer
{
    template<typename _Head, typename... _Tail>
    struct reverse_tuple_inner: reverse_tuple_outer<_N-1, _Head, _All...>::template reverse_tuple_inner<_Tail...> { };
};

template<typename... _All>
struct reverse_tuple_outer<0, _All...>
{
    template<typename... _Tail>
    struct reverse_tuple_inner {
        typedef std::tuple<_All...> type;
    };
};

template<typename... _Args>
struct reverse_tuple
{
    typedef typename reverse_tuple_outer<sizeof...(_Args)>::template reverse_tuple_inner<_Args...>::type type;
};

template<typename... _Args>
struct strip_and_reverse_tuple;

template<typename... _Args>
struct strip_and_reverse_tuple<std::tuple<_Args...>>
{
    typedef typename reverse_tuple<_Args...>::type type;
};

template<uint _N, typename... _Args>
struct partial_tuple
{
    typedef typename strip_and_reverse_tuple<typename reverse_tuple_outer<sizeof...(_Args)-_N>::template reverse_tuple_inner<_Args...>::type>::type type;
};

int main()
{
    //partial_tuple<1, std::string, std::string, int, int>::type A{"test", 5, 1};
    partial_tuple<1, std::string, std::string, int, int>::type B{"test", "test", 5};
}

追加のボーナスとして、reverse_tuple必要に応じて も持っています。

于 2011-07-09T08:14:59.737 に答える
0

Boost.MPL と Boost.Fusion を使用して同様のことを行いました。 などの MPL 機能を使用して型シーケンスを計算しpush_back、それをfusion::vectorwithfusion::as_vectorおよび MPL アダプターに変換します。fusion::vectorただし、 aをに変換するヘルパーが既にありましたstd::tuple

于 2011-07-09T04:06:43.273 に答える