-std = c ++ 98和OS X 10.10

时间:2015-01-19 14:56:54

标签: c++ macos g++ osx-yosemite clang++

我目前正在尝试使用OS X 10.10上的-std = c ++ 98标志编译我的程序:

clang++ -std=c++98 -pedantic -W -Wall -Werror *.cpp

g++ -std=c++98 -pedantic -W -Wall -Werror *.cpp

奇怪的是,当我使用OS 10.10进行编译时,没有显示任何错误,而有些则显示为GNU / Linux发行版。

使用GNU / Linux发行版我有一些错误,因为我使用s.open(file);而不是s.open(file.c_str());,但在OS X上没有错误,即使使用s.open(file);也是如此。

也许这个错误和每个操作系统的文件系统之间只有一个链接?

2 个答案:

答案 0 :(得分:1)

你是对的。这段代码不应该在C ++ 98下编译:

#include <fstream>
#include <string>

int main() {
    std::string file("/tmp/foo.txt");
    std::ifstream s;
    s.open(file);
}

它在技术上是libc ++中的一个错误,但是我怀疑它们是否会想要修复它。

如果您愿意,可以使用libstdc ++作为标准库进行编译,该库没有实现此功能(至少苹果分发的版本没有)。

[10:13am][wlynch@watermelon /tmp] clang++ -std=c++98 -stdlib=libstdc++ foo.cc
foo.cc:7:12: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'const char *'
    s.open(file);
           ^~~~
/usr/include/c++/4.2.1/fstream:518:24: note: passing argument to parameter '__s' here
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
                       ^
1 error generated.

答案 1 :(得分:1)

问题在于,在OS X上,您仍在使用标准库的C ++ 11实现,其中包括ostream::open方法接受std::strings等API更改。

使用-std=c++98更改C ++标准不会影响正在使用的标准库,并且libc ++不实现C ++ 98(例如,在C语言中没有#ifdef删除那些open(std::string) API ++ 98模式)而我认为libstdc ++在C ++ 98模式下构建时会隐藏非c ++ 98 API。