gpt4 book ai didi

c++ - `std::tuple` 有什么用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:16 28 4
gpt4 key购买 nike

此处 ( https://stackoverflow.com/a/37550660/34509 ) 用户 @Barry可以在评论区备注 std::tuple<int[2]>并且显然不禁止实例化这种类型。我还没有听说过这个野兽,我想知道它有什么用,而不是存储 int var[2]直接或使用 std::array<int, 2> .

据报道,std::tuple<int[2]>不可复制,不可移动,也不可从 int var[2] 构造.它还有什么其他用途?

最佳答案

我很确定这是未定义的行为。请参阅RequiresReturns 子句:

tuple.creation-10 and 12说:

Requires: For all i, Ui shall be the type cvi tuple<<em>Args<sub>i</sub>...</em>>, where cvi is the (possibly empty) ith cv-qualifier-seq and Argsi is the parameter pack representing the element types in Ui. Let Aik be the kith type in Argsi. For all Aik the following requirements shall be satisfied: If Ti is deduced as an lvalue reference type, then is_constructible<<em>A<sub>ik</sub></em>, <em>cv<sub>i</sub></em> <em>A<sub>ik</sub></em>&>::value == true, otherwise is_constructible<<em>A<sub>ik</sub></em>, <em>cv<sub>i</sub></em> <em>A<sub>ik</sub></em>&&>::value == true.

Returns: A tuple object constructed by initializing the kith type element eik in ei... with get<<em>k<sub>i</sub></em>>(std::forward<<em>T<sub>i</sub></em>>(<em>tp<sub>i</sub></em>)) for each valid ki and each group ei in order.

正如 Barry 所说,没有什么能阻止 std::tuple<int[2]> t;但是试图用它做任何事情可能会导致编译器出现硬错误。示例:

std::tuple<int[2]> t; // fine
std::tuple<int[2]> t{}; // fine

同时:

std::tuple<int[2]> a() {
int a[2] = { 1, 2};
return std::tuple<int[2]>(a);
}

int main() {
auto x = a();
// ...
}

给出如下错误:

error: array initializer must be an initializer list
: _M_head_impl(std::forward<_UHead>(__h)) { }

关于c++ - `std::tuple<int[N]>` 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37571816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com