相关疑难解决方法(0)

在OS X上,简单的C++程序给出了不正确的结果(这是命令行选项'c ++ 03'vs'c ++ 11'的结果)

这个简单的程序(在Linux上编译时)将根据是否编译而正确地给出两个不同的答案-std=c++0x.

问题:我无法在OS X(Mountain Lion,10.8 SDK)上重现同样的事情.我错过了什么?

#include <iostream>
#include <sstream>

class Thing : public std::ostringstream
{
public:
    Thing() : std::ostringstream() {}
    virtual ~Thing() { std::cerr << str(); }
};

int main(int argc, const char * argv[]) {
    Thing() << "Hello" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

要了解我的意思,请执行以下操作(首先在Linux上,看看它应该如何工作):

> g++ main.cpp
> ./a.out
0x401471
Run Code Online (Sandbox Code Playgroud)
> g++ -std=c++0x main.cpp
> ./a.out
Hello
Run Code Online (Sandbox Code Playgroud)

第一个将打印十六进制地址,第二个将打印"Hello".这是正确的行为,因为操作符<<解析为两个不同的东西(C++ 03中没有右值引用,所以你去了).

现在,在OS X上尝试相同的事情:


> xcrun c++ main.cpp
> ./a.out
0x10840dd88
Run Code Online (Sandbox Code Playgroud)

(这正确地产生十六进制输出.)


> xcrun c++ -std=c++0x main.cpp
> …
Run Code Online (Sandbox Code Playgroud)

c++ linux macos rvalue operator-keyword

8
推荐指数
2
解决办法
2716
查看次数

标签 统计

c++ ×1

linux ×1

macos ×1

operator-keyword ×1

rvalue ×1