Question about passing by const reference

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

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?
 
V

Victor Bazarov

christopher said:
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
 
C

christopher diggins

Victor Bazarov said:
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!
 
H

Howard Hinnant

Victor Bazarov said:
christopher said:
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/wg21/docs/cwg_active.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
 

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

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top