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>());

原因是第三个调用tuple(const Types&...),但这似乎是一个任意限制。

C++11 是否无法用可变参数模板表达这一点,还是有可能?

4

1 回答 1

3

这是可能的,但并非微不足道。为了使这个工作,一个包含N参数的元组必须支持2^N构造函数,所有的组合T&&T const&for each T

我们要做的是混合这些2^N构造函数,这些构造函数可以使用继承来完成。由于基类的构造函数只能using显式使用,因此我们只能添加固定数量的基类的构造函数,因此我们必须使用递归。

一种方法是从0to计数,2^N如果第 i 位为 1,则使第 i 个参数为 const-ref,否则为右值。总共使用基类执行此操作,2^N其中每个基类都向其直接基类添加一个构造函数。

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 回答