如何有效地实现 std::tuple,使得空类型的元组本身就是空类型
How to implement std::tuple efficiently such that a tuple of empty types is itself an empty type?
我正在实现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>
段错误。 这可能是您简化问题的人工制品,我不确定。
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 创建派生自可变参数模板包的类型元组
- 枚举元组类型的最佳方法?
- 我无法在用forward_as_tuple创建的元组中按类型访问元素
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 在C++中返回基元类型数组(Java比较)
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 如何将值元组中的相应值附加到相同类型的向量元组中
- 您自己的类型的结构化绑定,不是结构或元组(通过公共成员函数)
- 如何在编译器时动态获取元组内的变量类型C++?
- 验证(使用 static_assert)元组类型是否遵循某种顺序(有状态编译时检查)
- 使用每种类型的可变参数模板上的类模板初始化元组
- 在 c++17 中筛选类型元组
- 有没有办法将一对元组剥离为可变参数模板类型或实例化具有可变参数类型的东西?
- 从类型元组到通过调用模板函数返回的值数组
- 为什么不采用非类型部分专用元组?
- 以后可以解压缩类型元组吗?
- 如何根据表达式在Boost.Hana中是否有效来过滤类型元组