[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

istream ctor problem, _or_ compiler problem?

From: U.Mutlu
Subject: istream ctor problem, _or_ compiler problem?
Date: Sat, 15 Sep 2012 09:26:52 +0200
User-agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:14.0) Gecko/20120715 Firefox/14.0.1 SeaMonkey/2.11

why is the following code not compiling?


   Symptoms: mysterious compiler error in line 41 (marked as "BUG")

# g++ -Wall -O2 -o test2.exe test2.cpp
test2.cpp: In function ‘std::string myreader(const string&, const string&)’:
test2.cpp:41:45: error: invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘void*’

# g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.1-2' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.1 (Debian 4.7.1-2)

# uname -a
Linux s7 3.6.0-rc5-my1 #1 SMP Wed Sep 12 19:22:44 CEST 2012 x86_64 GNU/Linux

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
using namespace std;

string myreader(const string& AsFromBuf, const string& AsFromFile)
  { // reader for reading from buf -or- file, buf has precedence if both filled

    istringstream sbuf(AsFromBuf);
    ifstream      sfile; if (AsFromBuf.empty() && !AsFromFile.empty());

    // generic reader selector:
 // istream& si = sbuf;     // compiles ok
 // istream& si = sfile;    // compiles ok
    istream& si = sfile.is_open() ? sfile : sbuf;   // <--- BUG;  WHY IS THIS 

    // read it:
    string sRet, sLine;
    bool   fEof = false;
    while (!fEof)
        getline(si, sLine), fEof = si.eof();
        sRet += sLine + "\n";

    return sRet;

int main()
    string sBuf  = "blah foo\n";
    string sFile = "test.txt";

    const string s = myreader(sBuf, sFile);
    printf("%s\n", s.c_str());

    return 0;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]