What is `std :: tuple <int [N]>`?

Here ( https://stackoverflow.com/a/3184165/ ), @Barry user notes in the comments section that you can use std::tuple<int[2]> and that it is apparently not forbidden to create such a type. I have not heard about this beast yet, and I am interested in what it can use, unlike storing int var[2] directly or using std::array<int, 2> .

As reported, std::tuple<int[2]> cannot be copied, neither movable nor constructed from int var[2] . What other uses does it have?

+6
source share
1 answer

I am sure this behavior is undefined. See Suggestion Requires and Return:

tuple.creation-10 and 12 says:

Required: for all i, U i must be of type cv i tuple<Args i ...> , where cv i is (possibly empty) i th cv-qualifier-seq, and Args i is a package of parameters representing the types of elements in U i . Allow A ik - type k i th in Args i . For all A ik , the following requirements must be met: if T i is deduced as an lvalue reference type, then is_constructible<A ik , cv i A ik &>::value == true , otherwise it is_constructible<A ik , cv i A ik &&>::value == true .

Returns: a tuple object constructed by initializing an element of type k i th e ik to e i ... with get<k i >(std::forward<T i >(tp i )) for each valid k i , and each group e i in order.

According to Barry, there is nothing stopping std::tuple<int[2]> t; , but trying to do something with it is likely to cause a hard error in the 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 such as:

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

All Articles