【发布时间】:2014-09-15 07:33:19
【问题描述】:
运行以下从我的宠物项目中提取并使用 GCC 4.9.1(以及 4.8.1)编译的 MWE
#include <iostream>
#include <string>
#include <sstream>
class InputStringStream
{
public:
InputStringStream(const std::string& str) : istringstream(str), currentLine() {}
std::string readLine()
{
std::getline(istringstream, currentLine);
return currentLine;
}
private:
std::istringstream istringstream;
std::string currentLine;
};
int main()
{
std::string s = std::string("line1\nline2\nline3");
InputStringStream stream(s);
std::cout << stream.readLine() + "\n" + stream.readLine() + "\n" + stream.readLine() << std::endl;
return 0;
}
产生以下输出
line3
line2
line1
在我期待的时候
line1
line2
line3
我做错了什么?
附:使用 Apple LLVM 编译器 5.1 版编译的相同代码产生了我所期望的结果。 Visual C++ 2012 在 GCC 方面。
【问题讨论】:
-
只是为了重申这一点:您的问题本身错误地混淆了两个不相关的问题:运算符的关联性决定了表达式的 含义(如果您will),但您遇到的问题是关于子表达式的求值顺序。两者没有任何关系。
-
@KerrekSB,是的,现在我明白了。但是当我问这个问题时,我(错误地)认为问题是关于 operator+() 的关联性。所以我会保持原样,它可能会帮助其他人。
标签: c++ string gcc visual-c++-2012