473,396 Members | 2,039 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,396 software developers and data experts.

Question about passing by const reference

The following program doesn't work in gcc 3.3.3:

#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

void FuBar(const stringstream& s) {
cout << s.str() << endl;
}

int main(int argc, char *argv[])
{
FuBar(stringstream("hello"));
getchar();
return 0;
}

I get the message
`std::ios_base::ios_base(const std::ios_base&)' is private

Which really surprises me, because I don't understand why the copy
constructor should be called? Is this normal? What is the logic, why isn't
the temporary actually passed by reference?

--
Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org
Jul 23 '05 #1
3 1272
christopher diggins wrote:
The following program doesn't work in gcc 3.3.3:

#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

void FuBar(const stringstream& s) {
cout << s.str() << endl;
}

int main(int argc, char *argv[])
{
FuBar(stringstream("hello"));
getchar();
return 0;
}

I get the message
`std::ios_base::ios_base(const std::ios_base&)' is private

Which really surprises me, because I don't understand why the copy
constructor should be called? Is this normal? What is the logic, why isn't
the temporary actually passed by reference?


The Standard requires the copy constructor to be _callable_ but not
necessarily _called_. In some contexts a copy may be required. In your
example, a copy cannot be made and that's what the compiler complains
about. The compiler cannot conclude that "it's OK if the copy cannot be
made because I probably don't really need it that much".

V
Jul 23 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:iv_Xd.51949>
The Standard requires the copy constructor to be _callable_ but not
necessarily _called_. In some contexts a copy may be required. In your
example, a copy cannot be made and that's what the compiler complains
about. The compiler cannot conclude that "it's OK if the copy cannot be
made because I probably don't really need it that much".


Thank you very much Victor!

--
Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org
Jul 23 '05 #3
In article <iv*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> wrote:
christopher diggins wrote:
The following program doesn't work in gcc 3.3.3:

#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

void FuBar(const stringstream& s) {
cout << s.str() << endl;
}

int main(int argc, char *argv[])
{
FuBar(stringstream("hello"));
getchar();
return 0;
}

I get the message
`std::ios_base::ios_base(const std::ios_base&)' is private

Which really surprises me, because I don't understand why the copy
constructor should be called? Is this normal? What is the logic, why isn't
the temporary actually passed by reference?


The Standard requires the copy constructor to be _callable_ but not
necessarily _called_. In some contexts a copy may be required. In your
example, a copy cannot be made and that's what the compiler complains
about. The compiler cannot conclude that "it's OK if the copy cannot be
made because I probably don't really need it that much".


Actually future compilers will probably be required to conclude that it
is OK not to copy in this context. There's a cwg dr in review status at:

http://www.open-std.org/jtc1/sc22/wg...ctive.html#391

This DR will make the OP's example well formed.

In general, it is my hope that C++0X will be much friendlier to
non-copyable types.

-Howard
Jul 23 '05 #4

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

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
11
by: Daniel Wilcox | last post by:
I have a question, I have been given a piece of code that apparantly compiles under Visual C++ but I cannot get to compile under g++ 3.2. I have read the FAQ and delved into the Stroustrup book...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
12
by: wickwire | last post by:
I have created a class and used it to further overload ostream: class drum { ... friend ostream& operator<< ( ostream&, drum const& ); } ostream& operator<< ( ostream& out, drum const& od )...
5
by: Mike Cain | last post by:
Hi - I am looking for the most efficient way to pass a STL string from one function to another (using MS VS 7.0 ATL if that matters) and have a few questions abuot the principles at work here. ...
3
by: Giff | last post by:
Hi, I am trying to change my way of programming (I am still learning) , in particular I am putting an effort in passing const ref to functions, when possible. When possible means (to me) when...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.