473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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

Feb 22 '07 #1
2 3547
Eric Lilja wrote:
..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[i], rhs[i] );
}
}

#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
Feb 22 '07 #2
Eric Lilja wrote:
...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);

--
Clark S. Cox III
cl*******@gmail.com
Feb 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
2
by: barb28 | last post by:
Hello, sometimes I have noticed that in web addresses, the number: 20% will show up in the address. I think this is somehow related to a 'blank space', but am unsure, does anyone know about...
2
by: Hunter | last post by:
when I firstly ran "connect to dbname" command after "db2start", it spent me about 10 mins. I saw a process named "db2sync" that used 700M memory. The OS is Linux Advanced Server V2.1, Database...
13
by: (Pete Cresswell) | last post by:
Back when I was writing Mac/Pascal stuff, there was a little utility called "Molasses" that could be set to slow down the whole system by variying amounts. The idea was that if you're developing...
14
by: David W. Fenton | last post by:
I'm no stranger to this error message, but I have a client who is experiencing it, but, fortunately, without any actual data corruption, and it's driving them made. Their inability to grasp that...
11
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can anyone point out for me the reason? thanks !! ...
4
by: Thomas | last post by:
hi all, I have 2 classes, A and B. Each class has a member function that needs to access an element of the other class. Here's the code: #include <iostream> using namespace std; class B;
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
26
by: Prisoner at War | last post by:
Hello, Everyone: Is there any other values or whatever-it's-called to the cursor property in CSS?? So far, I only know of cursor:pointer (or, for MSIE, cursor:hand).... Any way to load in...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.