[英]What's the use of `std::tuple<int[N]>`?
Here ( https://stackoverflow.com/a/37550660/34509 ) user @Barry notes in the comment section that you can use std::tuple<int[2]>
and that it is not apparently forbidden to instantiate such a type. 这里( https://stackoverflow.com/a/37550660/34509 )用户@Barry在评论部分注意到你可以使用
std::tuple<int[2]>
并且显然禁止实例化这样的类型。 I have not yet heard about this beast and I wonder what uses it could have, as opposed to storing a int var[2]
directly or using std::array<int, 2>
. 我还没有听说过这个野兽,我想知道它有什么用处,而不是直接存储
int var[2]
或者使用std::array<int, 2>
。
Reportedly, std::tuple<int[2]>
is not copyable, neither movable nor construtable from an int var[2]
. 据报道,
std::tuple<int[2]>
不可复制,既不可移动也不可转换为int var[2]
。 What other uses does it have? 它有什么其他用途?
I'm pretty sure this is undefined behavior. 我很确定这是未定义的行为。 See the Requires and Returns clause:
请参阅Requires and Returns子句:
tuple.creation-10 and 12 says: tuple.creation-10和12说:
Requires: For all i , U i shall be the type cv i
tuple< Args i ... >
, where cv i is the (possibly empty) i th cv-qualifier-seq and Args i is the parameter pack representing the element types in U i .要求:对于所有i , U i将是类型cv i
tuple< Args i ... >
,其中cv i是(可能为空) i th cv-qualifier-seq和Args i是表示元素类型的参数包在你我 Let A ik be the k i th type in Args i .设A IK是第k 第 i 个类型在args 我 。 For all A ik the following requirements shall be satisfied: If T i is deduced as an lvalue reference type, then
is_constructible< A ik , cv i A ik &>::value == true
, otherwiseis_constructible< A ik , cv i A ik &&>::value == true
.对于所有A IK以下要求应当满足:如果T i被推导出一个左值引用类型,然后
is_constructible< A ik , cv i A ik &>::value == true
,否则is_constructible< A ik , cv i A ik &&>::value == true
。Returns: A
tuple
object constructed by initializing the k i th type element e ik in e i ... withget< k i >(std::forward< T i >( tp i ))
for each valid k i and each group e i in order.返回:
tuple
通过初始化第k构造的对象的 第 i 个类型的元素e IK在E I ...与get< k i >(std::forward< T i >( tp i ))
每个有效K I和各组我按顺序。
Like Barry says, there's nothing preventing std::tuple<int[2]> t;
就像巴里所说,没有什么能阻止
std::tuple<int[2]> t;
but trying to do anything with it is likely to cause a hard error in a compiler. 但尝试对它做任何事情都可能导致编译器出现硬错误。 Example:
例:
std::tuple<int[2]> t; // fine
std::tuple<int[2]> t{}; // fine
While: 而:
std::tuple<int[2]> a() {
int a[2] = { 1, 2};
return std::tuple<int[2]>(a);
}
int main() {
auto x = a();
// ...
}
gives errors like: 给出如下错误:
error: array initializer must be an initializer list
: _M_head_impl(std::forward<_UHead>(__h)) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.