简体   繁体   English

什么是`std :: tuple <int [N]>`的用法?

[英]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 . 要求:对于所有iU 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 , otherwise is_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 ... with get< k i >(std::forward< T i >( tp i )) for each valid k i and each group e i in order. 返回: tuple通过初始化第k构造的对象 i 类型的元素e IKE 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.

相关问题 std::stack 是什么意思<int,std::vector<int> &gt; s1; - What is the meaning of std::stack<int,std::vector<int>> s1; std::endl 和 '\n' 有什么区别 - What's the difference between std::endl and '\n' 有没有办法将 std::get(std::tuple) 与非常量 int 作为模板参数一起使用? - Is there a way to use std::get(std::tuple) with a non-const int as template argument? Apple 的 clang 不能将 &lt;=&gt; 与 std::tuple 一起使用 - Apple's clang can't use <=> with std::tuple 通过将std :: tuple <int,int>赋值给const std :: tuple <int,int>&来延长它的生命周期 - Extending lifetime of std::tuple<int&,int> by assigning it to const std::tuple<int, int>& 替换 std::tuple 的第 N 个元素 - Replace N-th element of a std::tuple 在编译时用已知的N重复到std :: tuple - repeat to std::tuple with known N at compile time std :: tr1 :: array的大小是多少 <int, 16> &gt; - what's the size of std::tr1::array<int, 16> > 对于C++向量初始化,“向量”有什么区别<int> v = n;”和“向量<int> v(n);"</int></int> - For C++ vector initialization, what's the difference between "vector<int>v = n;" and "vector<int>v(n);" 如何获取指向std :: get &lt;0,tuple的指针 <int,int> &gt;)? - How to get a pointer to std::get<0,tuple<int,int>>)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM