使用分配器 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&...);
您的实施已选择使用保留供其使用的名称。这些名称到底是什么对编译器来说并不重要。
what const _Tp& ... __t
is?
这是要复制到元组中的元素参数包。对于 std::tuple<int>
,它是 const int&
,对于 std::tuple<std::string, bool, char>
,它是 const std::string &, const bool &, const char &
。 __t
是参数包的名称。 C++ 允许模板具有不同数量的参数。
what about tuple(_Up&&... __u)
?
这是超载 (3)
Converting constructor. Initializes each element of the tuple with the corresponding value in std::forward<UTypes>(args)
.
This overload only participates in overload resolution if sizeof...(Types) == sizeof...(UTypes)
and sizeof...(Types) >= 1
and std::is_constructible<Ti, Ui&&>::value
is true
for all i
.
The constructor is explicit if and only if std::is_convertible<Ui&&, Ti>::value
is false
for at least one 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
不参与其构造。参见 Uses-allocator construction
我正在学习 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&...);
您的实施已选择使用保留供其使用的名称。这些名称到底是什么对编译器来说并不重要。
what
const _Tp& ... __t
is?
这是要复制到元组中的元素参数包。对于 std::tuple<int>
,它是 const int&
,对于 std::tuple<std::string, bool, char>
,它是 const std::string &, const bool &, const char &
。 __t
是参数包的名称。 C++ 允许模板具有不同数量的参数。
what about
tuple(_Up&&... __u)
?
这是超载 (3)
Converting constructor. Initializes each element of the tuple with the corresponding value in
std::forward<UTypes>(args)
.This overload only participates in overload resolution if
sizeof...(Types) == sizeof...(UTypes)
andsizeof...(Types) >= 1
andstd::is_constructible<Ti, Ui&&>::value
istrue
for alli
.The constructor is explicit if and only if
std::is_convertible<Ui&&, Ti>::value
isfalse
for at least onei
.
例如对于 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
不参与其构造。参见 Uses-allocator construction