user6420285
user6420285

Reputation:

std::swap not working with std::array

I have some code that requires me to put some data into a std::array. I thought I could do this by swapping two arrays and discarding one of them. Here's the code

int main()
{
    std::array<double, 10> a;
    std::array<double, 5> b;
    /*populate b*/
    /*swap them round*/
    std::swap(a, b);
}

But I get a very strange compiler error (MSVC2013).

CashFlows.cpp(27): error C2665: 'std::swap' : none of the 3 overloads could convert all the argument types
include\exception(502): could be 'void std::swap(std::exception_ptr &,std::exception_ptr &)'
include\tuple(572): or 'void std::swap(std::tuple<> &,std::tuple<> &)'
include\thread(232): or 'void std::swap(std::thread &,std::thread &) throw()'
while trying to match the argument list '(std::array<_Ty,_Size>, std::array<_Ty,_Size>)'
with
[
  _Ty=double,
  _Size=0x0a
]
and
[
  _Ty=double,
  _Size=0x05
]

which I don't understand. What does std::tuple<> etc. have anything to do with this?

Upvotes: 4

Views: 1927

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

which I don't understand. What does std::tuple<> etc. have anything to do with this?

You just sort out reading error messages a bit further upon hints. The compiler just lists the available constructor declarations that could be possibly appropriate:

could be

void std::swap(std::exception_ptr &,std::exception_ptr &)

or

void std::swap(std::tuple<> &,std::tuple<> &)

or

void std::swap(std::thread &,std::thread &) throw()

You can see these also documented here.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234665

The objects a and b are fundamentally different types as they are different template instantiations.

Therefore you can't use them together in std::swap as the arguments must be the same type.

Your compiler can't find an appropriate overload (granted, the ones it shows you do seem odd), so issues the error that you see.

Upvotes: 8

Related Questions