http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #28188|0                           |1
        is obsolete|                            |

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-09-30 
10:58:18 UTC ---
Comment on attachment 28188
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28188
Testcase for Bug "error: invalid initialization of non-const reference of type
‘std::istream&"

(In reply to comment #9)
> Created attachment 28188 [details]
> Testcase for Bug "error: invalid initialization of non-const reference of type
> ‘std::istream&"
> 
> Mysterious compiler error in line 35 of the attached testcase (test1.cpp);
> marked there as "BUG":
> 
> test1.cpp:35:45: error: invalid initialization of non-const reference of type
> ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘void*’

That's not mysterious, and is not related to this bug report.

Your code can be reduced to:

struct Base {
  operator void*() { return 0; }
};
struct Derived1 : Base { };
struct Derived2 : Base { };

Derived1 d1;
Derived2 d2;

Base& b = true ? d1 : d2;

The conditional expression cannot implicitly convert two objects of different
types to a common base class, so instead the conversion operator is used to
convert both types to void*

To force the behaviour you expect use an explicit cast on one operand:

istream& si = sfile.is_open() ? (std::istream&)sfile : sbuf;

Reply via email to