I’m trying to read a single character from a stream. With the following code I get a “ambiguous overload” compiler error (GCC 4.3.2, and 4.3.4). What I’m doing wrong?
#include <iostream>
#include <sstream>
int main()
{
char c;
std::istringstream("a") >> c;
return 0;
}
Remarks:
- Visual Studio 2008 compiles without errors
- Other types (
int,double) are working - If I first create a variable
std::istringstream iss("a"); iss >> c, the compiler gives no error
The extraction operator
>>for characters is a non-member function template:Since this takes its first argument by non-
constreference, you can’t use a temporary rvalue there. Therefore, your code cannot select this overload, only the various member function overloads, none of which match this usage.Your code is valid in C++11, because there is also an extraction operator taking an rvalue reference as the first argument.
One of that compiler’s many non-standard extensions is to allow temporary rvalues to be bound to non-
constreferences.Most extraction operators for fundamental types are member functions, which can be called on a temporary rvalue.
issis a non-temporary lvalue, so it can be bound to a non-constreference.