简体   繁体   中英

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. 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> .

Reportedly, std::tuple<int[2]> is not copyable, neither movable nor construtable from an int var[2] . What other uses does it have?

I'm pretty sure this is undefined behavior. See the Requires and Returns clause:

tuple.creation-10 and 12 says:

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 . Let A ik be the k i th type in Args i . 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 .

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.

Like Barry says, there's nothing preventing 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)) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM