file >> out.rdbuf(); fails to compile, why?

S

Siemel Naran

My compiler (Borland C++) fails to compile this code:

ifstream file(filename.c_str());
ostringstream out;
file >> out.rdbuf();

with the error in the 3rd line above that:
[C++ Error] realmain.cpp(83): E2015 Ambiguity between
'_STL::basic_istream<char,_STL::char_traits<char> >::eek:perator >>(bool &)'
and '_STL::basic_istream<char,_STL::char_traits<char> >::eek:perator >>(void *
&)'


But it compiles the following without error.

ifstream file(filename.c_str());
ostringstream out;
streambuf * outbuf = out.rdbuf();
file >> outbuf;


Why the discrepancy?
 
B

Buster

Siemel said:
My compiler (Borland C++) fails to compile this code:

ifstream file(filename.c_str());
ostringstream out;
file >> out.rdbuf();

with the error in the 3rd line above that:
[C++ Error] realmain.cpp(83): E2015 Ambiguity between
'_STL::basic_istream<char,_STL::char_traits<char> >::eek:perator >>(bool &)'
and '_STL::basic_istream<char,_STL::char_traits<char> >::eek:perator >>(void *
&)'

The 'std::istream::eek:perator >> (std::streambuf *)' overload is a better
match than either of those. I suspect it's missing.
But it compiles the following without error.

ifstream file(filename.c_str());
ostringstream out;
streambuf * outbuf = out.rdbuf();
file >> outbuf;

Did you run it? I imagine that ">>" is calling 'std::istream::eek:perator
I tried to test this hypothesis using Borland C++ Builder 6.0 and found
that it can't read raw pointers either. So you're on to a loser. Here's
the test.

#include <istream>
#include <ostream>
#include <sstream>
#include <iostream>
#include <limits>

int main ()
{
void * p = reinterpret_cast <void *> (0xdeadbeef);
void * q;

std::eek:stringstream out;
out << p;

std::istringstream in (out.str ());

if (in >> q) std::cout << std::hex << q << '\n';
else std::cout << "fail\n";

// pause to view results if running from the Borland IDE
std::cin.ignore (std::numeric_limits <std::streamsize>::max (), '\n');
}

Outputs "0xdeadbeef" on g++ 3.3.3, "fail" on bcb 6.0.
Why the discrepancy?

Your code is pretty much correct. Complain to Borland, and/or upgrade to
the latest version.
 
O

Old Wolf

Buster said:
int main ()
{
void * p = reinterpret_cast <void *> (0xdeadbeef);
void * q;

std::eek:stringstream out;
out << p;

Undefined behaviour -- using the value of an invalid pointer
std::istringstream in (out.str ());

if (in >> q) std::cout << std::hex << q << '\n';
else std::cout << "fail\n";

// pause to view results if running from the Borland IDE
std::cin.ignore (std::numeric_limits <std::streamsize>::max (), '\n');
}

Outputs "0xdeadbeef" on g++ 3.3.3, "fail" on bcb 6.0.
Since you have invoked UB, "fail" is a conforming output.
 

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
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top