Why the C++ code below can't be compiled?
#include <utility>
int main() {
int x[6];
int y[6];
std::pair<int[6], int[6]> a(x, y);
return 0;
}
For example MSVC gives the following error:
error C2661: 'std::pair<int [6],int [6]>::pair': no overloaded function takes 2 argument
Thanks for the comments that using C++ alternatives to construct arrays, but I also want to know the reason that the code can't be compiled.
Using
int[]as a type of element in astd::pairis actually allowed.For example you could use:
What's not valid is the initialization of the pair with
(x,y). C arrays are not copy assignable: you cannot assign some arraya2to anothera1by the trivial assignmenta1 = a2;. And C arrays decay into pointers when passed to a funtion/method.However, the prefered way to do it in C++ is to use
std::array:Note the usage of
using(providing a type alias) to make the code more readable.