使用分配器 c++ 创建元组
Creating tuple with allocator c++
我正在C++中查找元组,现在我正在尝试使用 libcxx 的分配器创建元组
template <class _Alloc>
LIBCPP_INLINE_VISIBILITY
tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
例如:
std::allocator<int> myAllocator;
std::tuple<int> t(std::allocator_arg, myAllocator, 2);
但似乎上面的字符串称为
template <class Alloc>
tuple(allocator_arg_t, const Alloc& a, const Types&...);
我应该为此更改什么?
同样,有一行我不清楚:
explicit
tuple(_Up&&... __u)
这怎么叫?
当您查看实现的源代码并查看
namespace std {
// Other things
template <typename ... _Tp>
class tuple {
// More things
template <class _Alloc>
LIBCPP_INLINE_VISIBILITY
tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
// an implementation of this constructor
};
}
那是cppreference 命名的构造函数
template <class Alloc>
tuple(allocator_arg_t, const Alloc& a, const Types&...);
您的实现已选择使用保留供其使用的名称。这些名称究竟是什么对编译器来说并不重要。
什么是
const _Tp& ... __t
?
它是要复制到元组中的元素的参数包。对于std::tuple<int>
,它是const int&
,对于std::tuple<std::string, bool, char>
它是const std::string &, const bool &, const char &
。__t
是参数包的名称。C++允许模板具有不同数量的参数。
tuple(_Up&&... __u)
呢?
这就是过载 (3)
转换构造函数。使用
std::forward<UTypes>(args)
中的相应值初始化元组的每个元素。仅当
sizeof...(Types) == sizeof...(UTypes)
和sizeof...(Types) >= 1
并且对所有i
true
std::is_constructible<Ti, Ui&&>::value
时,此重载才参与过载解决。构造函数是显式的,当且仅当
std::is_convertible<Ui&&, Ti>::value
至少false
一个i
。
例如,对于std::tuple<int> tup('a');
,tup
将通过匹配UTypes...
与char
进行初始化,第一个成员的数值为'a'
(大多数平台上为97)。
请注意,对std::tuple<int>
使用分配器感知构造函数没有多大意义,因为int
不是分配器感知类型。这些构造函数存在于以下情况下
using statefully_allocated = std::vector<int, my_stateful_allocator<int>>;
my_stateful_allocator<int> alloc1 = /* something */
statefully_allocated source(alloc);
my_stateful_allocator<int> alloc2 = /* something else */
std::tuple<statefully_allocated, char> tup(std::allocator_arg, alloc2, source, 'a');
其中statefully_allocated
成员复制source
的内容,但使用alloc2
的副本进行分配。char
成员只是一个普通的char
,alloc2
参与其构造。请参阅用途分配器构造
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 创建派生自可变参数模板包的类型元组
- 如何在C++中创建元组向量?
- 我无法在用forward_as_tuple创建的元组中按类型访问元素
- 如何使用返回第 n 个元素的方法创建元组
- 从 c++ 中的字符串向量创建一个元组
- C++ 从模板参数创建元组向量
- 从可调用可变参数元组中的函数结果创建元组
- 为模板中的元组创建递归函数
- 通过移动从两个向量创建元组向量
- 动态创建和扩展STD ::元组为参数包
- 创建对满足特定谓词的对象的引用元组
- 从没有预处理器的HANA元组中创建功能签名
- 使用参数包和元组创建一个简单的表达式类
- C++向量元组,请按索引从元素创建元组
- 用于创建integral_constants的 aribtrary 元组的通用实用程序
- 使用分配器 c++ 创建元组
- 从可调用元组的结果创建元组
- 如何创建元组列表C++
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例