std::string in a .c file

I've seen std::string used in a .c file. std is a c++ namespace and namespaces were introduced in c++. Why is that so? Shouldn't it throw an error?


Yes, it will cause numerous compiler errors if it's compiled as C code. If it's being compiled as C++ instead, then it will compile fine. For example, GCC has the -x option to select the language to compile as, so you can compile a .c as C++ if you want with -x c++ . Likewise, the Microsoft Visual C++ compiler has the options /Tc and /Tp to select a source language of C and C++ respectively.

I suggest you fix your build system so that it doesn't pass the -x c++ or /Tp flag to files not ending with typical C++ source file extensions ( .cc , .cpp , .cxx , c++ , and .C , though the last three are quite rare).


The extension of a file is only there to help you as the reader. The compiler does not care as long as you use a c++ compiler.

链接地址: http://www.djcxy.com/p/86310.html

上一篇: 从图像解码穿孔带

下一篇: std :: string在.c文件中