"Swapping" two arrays like this doesn't compile because..

E

Eric Lilja

...during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?

The code (that only compiles if you comment out the call to
swap_arrays()):
#include <iostream>
#include <iterator>
using namespace std;

void swap_arrays(int *&a1, int *&a2)
{
int *ptr = a1;
a1 = a2;
a2 = ptr;
}

int main()
{
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);

cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}
 
K

Kai-Uwe Bux

Eric said:
..during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?

I don't think so.
The code (that only compiles if you comment out the call to
swap_arrays()):
#include <iostream>
#include <iterator>
using namespace std;

void swap_arrays(int *&a1, int *&a2)
{
int *ptr = a1;
a1 = a2;
a2 = ptr;
}

That function will not swap arrays. It swaps objects of type pointer-to-int.
int main()
{
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);

You promised int*& as the parameter. What you pass is int&.
cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}


Try this:


#include <cstddef>
#include <algorithm>

using namespace std;

template < typename T, std::size_t N >
void swap ( T (&lhs) [N], T (&rhs) [N] ) {
for ( std::size_t i = 0; i < N; ++i ) {
swap( lhs, rhs );
}
}

#include <iostream>
#include <iterator>

using namespace std;

int main ( void ) {
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap( array1, array2 );

cout << "array1 after swap:" << endl;

copy(array1, array1 + sizeof(array1) / sizeof(array1[0]),
ostream_iterator<int>(cout, " "));

cout << endl << "array2 after swap:" << endl;

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterator<int>(cout, " "));

cout << endl;
}


Best

Kai-Uwe Bux
 
C

Clark S. Cox III

Eric said:
...during the call to swap_arrays the name of the array decays to a
pointer to its first element and that pointer is a temporary variable
and you cant bind a non-const reference to a temporary. Correct
analysis?

Yes and no.

....
void swap_arrays(int *&a1, int *&a2) ....
int array1[] = {1, 2, 3};
int array2[] = {4, 5, 6};

swap_arrays(array1[0], array2[0]);

As it is, you are passing two (int) objects where to (int*) objects are
expected. Though, even if you change this to what I *assume* you
actually meant:

swap_arrays(array1, array2);

then your above analysis is correct, and the code still won't work. You
would have the same problem with:

void swap_double(double &d1, double &d2)
{
double temp = d1;
d1 = d2;
d2 = temp;
}

....

float f1 = 1, f2 = 2;
swap_double(f1,f2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top