473,800 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(arr ay1[0], array2[0]);

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

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

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

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

cout << endl;
}

Feb 22 '07 #1
2 3567
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(arr ay1[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_iterato r<int>(cout, " "));

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

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterato r<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_iterato r<int>(cout, " "));

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

copy(array2, array2 + sizeof(array2) / sizeof(array2[0]),
ostream_iterato r<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(arr ay1[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(arr ay1, array2);

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

void swap_double(dou ble &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
3000
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 buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried resize() passing a smaller size than the
2
37148
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 this? I have seen it mostly I think with netscape,
2
5796
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 is V8.1.5. The machine is Compaq ProLiant 8000, 1G Mem. How can I do? thanks..
13
1602
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 on a really fast box, you want to get an idea of how the app will run on a slower machine. I find myself in a somewhat-similar situation now. My little home LAN is really fast (should be...there's nothing much going over it...) but the...
14
9752
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 the cause is fundamentally a networking problem is also making me pull my hair out. Some history: The network is composed of five computers dating back to 1995, each purchased one at a time, and each having a different OS, etc. Until
11
2706
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 !! class AA {
4
1729
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
2859
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 apply. Can somebody tell me why? Thanks, Jess
26
2544
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 one's own somehow?? Just a noob at this...TIA!!
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10251
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.