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

Discussion in 'C++' started by Siemel Naran, Dec 17, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    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?
    Siemel Naran, Dec 17, 2004
    1. Advertisements

  2. Siemel Naran

    Buster Guest

    The 'std::istream::eek:perator >> (std::streambuf *)' overload is a better
    match than either of those. I suspect it's missing.
    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.
    Your code is pretty much correct. Complain to Borland, and/or upgrade to
    the latest version.
    Buster, Dec 17, 2004
    1. Advertisements

  3. Siemel Naran

    David Harmon Guest

    David Harmon, Dec 17, 2004
  4. Siemel Naran

    Old Wolf Guest

    Undefined behaviour -- using the value of an invalid pointer
    Since you have invoked UB, "fail" is a conforming output.
    Old Wolf, Dec 20, 2004
  5. Siemel Naran

    Buster Guest

    Oh, don't be silly. Change it to:
    Buster, Dec 22, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.