2

このコードは次のようにコンパイルされます。

pair <pair<int,int>,unique_ptr<int>> t({0,0}, unique_ptr<int>());

同様にそのように:

tuple<pair<int,int>,unique_ptr<int>> t(make_pair(0,0), unique_ptr<int>());

しかし、これはしません:

tuple<pair<int,int>,unique_ptr<int>> t({0,0}, unique_ptr<int>());

その理由は、3 つ目が を呼び出すためですがtuple(const Types&...)、恣意的な制限のようです。

C++ 11 は可変個引数テンプレートでこれを表現できませんか、それとも可能ですか?

4

1 に答える 1

3

それは可能ですが、自明ではありません。これを機能させるには、N引数を含むタプルが2^Nコンストラクタ、T&&およびT const&for eachのすべての組み合わせをサポートする必要がありますT

私たちがしなければならないことは、2^N継承を使用して実行できるこれらのコンストラクターを混在させることです。基本クラスのコンストラクターはusing明示的にしか使用できないため、一定数の基本クラスのコンストラクターしか追加できないため、再帰を使用する必要があります。

1 つのアプローチは、から数えて02^Ni 番目のビットが 1 の場合は i 番目のパラメーターを const-ref にするか、そうでない場合は右辺値にすることです。これを2^N、それぞれが直接のベースに 1 つのコンストラクターを追加する合計ベース クラスで行います。

namespace detail {
  // A bitlist holds N powers of two: 1, 2, 4, 8, 16, ...
  template <std::size_t... i> struct bitlist { using type = bitlist; };
  template <std::size_t N, typename=bitlist<>>
  struct make_bitlist;  
  template <std::size_t N, std::size_t... i>
  struct make_bitlist<N, bitlist<i...>>
    : make_bitlist<N-1, bitlist<0,1+i...>> {};
  template <std::size_t... i> struct make_bitlist<0, bitlist<i...>>
    : bitlist<(1<<i)...> {};

  struct forward_tag {}; // internal struct that nobody else should use

  // if T is a reference, some constructors may be defined twice, so use a non-accessible type.
  template <bool B, typename T>
  using const_if_set = typename std::conditional<B,
    typename std::conditional<std::is_reference<T>::value, forward_tag, T const&>::type, T&&>::type;

  // Our helper class.  Each tuple_constructor is derived from N-1 others
  // each providing one constructor.  N shall equal (1<<sizeof...(T))-1
  template <std::size_t N, typename L, typename... T> struct tuple_constructor;

  template <std::size_t N, std::size_t... I, typename... T>
  struct tuple_constructor<N, bitlist<I...>, T...>
    :  tuple_constructor<N-1, bitlist<I...>, T...>
  { // inherit base constructors
    using tuple_constructor<N-1, bitlist<I...>, T...>::tuple_constructor;
    tuple_constructor(const_if_set<(N & I), T>... t)
      : tuple_constructor<N-1, bitlist<I...>, T...>
          (forward_tag{}, std::forward<const_if_set<(N & I), T>>(t)...) {}
  };

  // base case: N=0, we finally derive from std::tuple<T...>
  template <std::size_t... I, typename... T>
  struct tuple_constructor<0, bitlist<I...>, T...> : std::tuple<T...> {
    tuple_constructor(T&&... t)
      : tuple_constructor(forward_tag{}, std::forward<T&&>(t)...) {}

    // All constructor calls are forwarded to this one
    template <typename... T2>
    tuple_constructor(forward_tag, T2&&... t2)
      : std::tuple<T...>(std::forward<T2>(t2)...) {}
  };

  // Convenience using for N=2^n, bitlist=1,2,4,...,2^n where n = sizeof...(T)
  template <typename... T>
  using better_tuple_base = tuple_constructor
    < (1<<sizeof...(T)) - 1, typename make_bitlist<sizeof...(T)>::type,  T... >;
}

template <typename... T> struct better_tuple : detail::better_tuple_base<T...> {
  using typename detail::better_tuple_base<T...>::tuple_constructor;
};

LWSでライブ

ただし、これは大きなタプルにうまく対応できず、コンパイル時間が大幅に増加することに注意してください。私の意見では、これは言語の制限です。

于 2013-03-28T23:39:35.000 に答える