如何有效地实现 std::tuple,使得空类型的元组本身就是空类型

How to implement std::tuple efficiently such that a tuple of empty types is itself an empty type?

本文关键字:类型 元组 实现 有效地 std tuple      更新时间:2023-10-16

我正在实现std::tuple,我希望它在对象大小和编译时都尽可能高效。我正在遵循这里和这里提出的建议。

为了提高编译时性能,该实现不使用递归继承,而是使用具有tuple_leaf技巧的多重继承。此外,它还尽可能使用空基类优化来减小类型的大小。

为了确保始终应用空基类优化,我的tuple实现本身派生自基类,而不是将实现存储在成员变量中。但是,这会导致嵌套元组出现问题,因为tuple_leaf技术通过转换为基类来工作。嵌套元组会导致歧义,因为同一类型的tuple_leaf在派生链中可能多次出现。

下面发布了一个说明简化问题的程序。有没有一种简单的方法来消除转换的歧义并允许程序在不抛出assert的情况下编译和执行?我已经考虑过检测嵌套元组大小写并以某种方式编码其类型中每个tuple_leaf的多维位置,但这看起来很复杂,并且可能会降低编译时性能。

#include <type_traits>
#include <cassert>
template<int i, class T, bool = std::is_empty<T>::value>
struct tuple_leaf
{
  tuple_leaf(T x) : val(x) {}
  T& get() { return val; }
  T val;
};

template<int i, class T>
struct tuple_leaf<i,T,true> : private T
{
  tuple_leaf(T x) : T(x) {}
  T& get() { return *this; }
};

template<int i, class T1, class T2>
struct type_at
{
  using type = T1;
};

template<class T1, class T2>
struct type_at<1,T1,T2>
{
  using type = T2;
};

template<class T1, class T2>
struct tuple_base : tuple_leaf<0,T1>, tuple_leaf<1,T2>
{
  tuple_base(T1 a, T2 b) : tuple_leaf<0,T1>(a), tuple_leaf<1,T2>(b) {}
  template<int i>
  tuple_leaf<i,typename type_at<i,T1,T2>::type> get_leaf()
  {
    // XXX how to disambiguate this conversion?
    return *this;
  }
};

// XXX deriving from tuple_base rather than
//     making tuple_base a member is the root of the issue
template<class T1, class T2>
struct my_tuple : tuple_base<T1,T2>
{
  my_tuple(T1 a, T2 b) : tuple_base<T1,T2>(a, b) {}
};
template<int i, class T1, class T2>
typename type_at<i,T1,T2>::type& get(my_tuple<T1,T2>& t)
{
  return (t.template get_leaf<i>()).get();
}
template<class T1,class T2>
my_tuple<T1,T2> make_tuple(T1 a, T2 b)
{
  return my_tuple<T1,T2>(a,b);
}
struct empty {};
int main()
{
  auto tuple = make_tuple(empty(), make_tuple(empty(),empty()));
  assert((std::is_empty<decltype(tuple)>::value));
  assert(sizeof(tuple) == sizeof(empty));
  get<0>(tuple);
  return 0;
}

编译器输出:

$ clang-3.5 -std=c++11 repro.cpp 
repro.cpp:47:12: error: ambiguous conversion from derived class 'tuple_base<empty, my_tuple<empty, empty> >' to base class 'tuple_leaf<0, empty, true>':
    struct tuple_base<struct empty, struct my_tuple<struct empty, struct empty> > -> tuple_leaf<0, struct empty>
    struct tuple_base<struct empty, struct my_tuple<struct empty, struct empty> > -> tuple_leaf<1, struct my_tuple<struct empty, struct empty> > -> struct my_tuple<struct empty, struct empty> -> tuple_base<struct empty, struct empty> -> tuple_leaf<0, struct empty>
    return *this;
           ^~~~~
repro.cpp:63:22: note: in instantiation of function template specialization 'tuple_base<empty, my_tuple<empty, empty> >::get_leaf<0>' requested here
  return (t.template get_leaf<i>()).get();
                     ^
repro.cpp:80:3: note: in instantiation of function template specialization 'get<0, empty, my_tuple<empty, empty> >' requested here
  get<0>(tuple);
  ^
1 error generated.

当你只有一把锤子时,一切看起来都像"为什么不试试CRTP"。 因为 CRTP 解决了 template 的所有问题。

使用派生的class D扩展tuple_leaf,并将tuple_base类型传入。 (或者,编写一个template<class...>struct types{};并将其传入 - 您所需要的只是一个唯一区分两个不同元组的类型(。

修改get_leaf以获得适当的类,现在没有歧义。

问题:

首先,如果没有ICF,这使得一堆现在相同的方法变得不同。

其次,如果你实现递归元组,这会严重破坏。 上面依赖于这样一个事实,即包含子元组 X 的元组具有与子元组不同的类型集。

第三,当我自己尝试使用上面的代码时,我得到了非 1 大小的空结构。 奇怪。 如果我绕过静态断言等,get<0>段错误。 这可能是您简化问题的人工制品,我不确定。