问题描述
尝试对临时流对象使用 getline()
时遇到意外的编译错误:
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;
}
}
看起来没有重载 getline()
存在,接受对流对象的右值引用。如果我改变 main()
使用一个左值,它会编译并按预期运行:
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;
}
}
所以我看了一下C ++ 11标准和我发现(§21.4.8.9), getline()
的重载,它需要一个流对象的右值引用 。
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.
我缺少明显的东西,或者这是一个错误?使用GCC 4.7.2和Clang 3.2时都会出现错误。
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.
推荐答案
如果我在OS X上用下面的代码编译,它会编译成功。您使用的是什么版本的libstdc ++或libc ++?
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 ++(和libc ++)还没有完全实现C ++ 2011标准库。这似乎是libstdc ++中缺少的函数之一。
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.
这篇关于显然,在GCC 4.7.2和Clang 3.2中,使用RRef的流去掉getline()的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!