繁体   English   中英

-std = c ++ 98和OS X 10.10

[英]-std=c++ 98 and OS X 10.10

我当前正在尝试在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); 而不是s.open(file.c_str()); 但是在OS X上,即使使用s.open(file);也没有错误s.open(file);

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

没错 该代码不应在C ++ 98下编译:

#include <fstream>
#include <string>

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

从技术上讲,这是libc ++中的一个错误,但是我怀疑他们是否想修复该错误。

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

[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.

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM