问题
I ran into an unexpected compilation error when trying to use getline()
with a temporary stream object:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string input = "hello\nworld\nof\ndelimiters";
string line;
if (getline(stringstream(input), line)) // ERROR!
{
cout << line << endl;
}
}
It looks like no overload of getline()
exists that accepts an rvalue reference to a stream object. If I change main()
to use an lvalue, it compiles and runs as expected:
int main()
{
string input = "hello\nworld\nof\ndelimiters";
string line;
stringstream ss(inpupt);
if (getline(ss, line)) // OK
{
cout << line << endl;
}
}
So I had a look in the C++11 Standard and I found out (§ 21.4.8.9) that an overload of getline()
that takes an rvalue reference to a stream object should be present.
Am I missing something obvious, or is this a bug? The error occurs both with GCC 4.7.2 and with Clang 3.2. I cannot test this on VC at the moment.
回答1:
If I compile on OS X with the following line, it compiles successfully. What version of the libstdc++ or libc++ are you using?
clang++ -std=c++11 -stdlib=libc++ foo.cc
libstdc++ (and libc++ for that matter) do not yet fully implement the C++ 2011 standard library. This appears to be one of the missing functions from libstdc++.
Sadly, I don't know of any resource that lists exactly what is missing from each implementation.
来源:https://stackoverflow.com/questions/14574486/apparently-missing-overload-of-getline-taking-rref-to-stream-in-gcc-4-7-2-and