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

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

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> >::operator >>(bool &)'
and '_STL::basic_istream<char,_STL::char_traits<char> >::operator >>(void *
&)'
But it compiles the following without error.

ifstream file(filename.c_str());
ostringstream out;
streambuf * outbuf = out.rdbuf();
file >> outbuf;
Why the discrepancy?
Jul 22 '05 #1
4 2169
Siemel Naran wrote:
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> >::operator >>(bool &)'
and '_STL::basic_istream<char,_STL::char_traits<char> >::operator >>(void *
&)'
The 'std::istream::operator >> (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::operator
(void * &)', and trying to read a raw pointer.


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::ostringstream 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.

--
Regards,
Buster
Jul 22 '05 #2
On Fri, 17 Dec 2004 10:29:18 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote,
My compiler (Borland C++) fails to compile this code:

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


You wanted the incantation
out << file.rdbuf();

http://groups.google.com/gr*********...ing.google.com
http://groups.google.com/gr*************************@ID-79865.news.uni-berlin.de

Jul 22 '05 #3
Buster wrote:
int main ()
{
void * p = reinterpret_cast <void *> (0xdeadbeef);
void * q;

std::ostringstream 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.

Jul 22 '05 #4
Old Wolf wrote:
Buster wrote:
int main ()
{
void * p = reinterpret_cast <void *> (0xdeadbeef);
void * q;

std::ostringstream out;
out << p;

Undefined behaviour -- using the value of an invalid pointer


Oh, don't be silly. Change it to:
"int x; void * p = reinterpret_cast <void *> (& x);"
and it still won't work.
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.

Jul 22 '05 #5

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

Similar topics

5
by: Michael Brennan | last post by:
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
0
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
13
by: Jason Heyes | last post by:
Is this all it takes to copy a file in C++? Is there any error handling missing in this function? bool copy_file(std::string first, std::string second) { std::ifstream in(first.c_str()); if...
0
by: Kirk | last post by:
I have created an application that copies images I select to two printer que's. The images are either AutoCAD plot files (.PLT) or scanned images (.TIF). My method works well for the PLT files on...
18
by: panig | last post by:
how the program knows when a file is finsihed, mmm?
0
by: =?Utf-8?B?UmFqaXYgRGFz?= | last post by:
Hi, I am able to view the contents of a remote folder in explorer, but am unable to enumerate content through c# console app? What could be the cause and fix? foreach (String s in...
8
by: Johnny | last post by:
Hi all: I have an ASP.NET form that reads an Excel file and populates a datagrid. If I load the form in IE on the server, and select a local file, the code works fine. However if I load the form...
7
by: sudhaMurugesan | last post by:
Hi all, I use asp.net with c#. I upload a file and save in a directory. I want to reopen the file so i check for file exists and then open it. But though the file exists, the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.