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?
- 1File extensions don't matter, I can write code in a *.mp3 file, and my compiler will happily compile it as C++ if I ask it to.Praetorian– Praetorian2012-10-22 23:22:55 +00:00Commented Oct 22, 2012 at 23:22
- 3@Praetorian: But maybe not a .png file.James McNellis– James McNellis2012-10-22 23:28:13 +00:00Commented Oct 22, 2012 at 23:28
- @JamesMcNellis, I think there's a video somewhere of someone imaging a BMP or something and changing the extension to a working C++ program.Qaz– Qaz2012-10-22 23:33:16 +00:00Commented Oct 22, 2012 at 23:33
- @chris Watch the gif from the second answer in James' linkPraetorian– Praetorian2012-10-22 23:37:47 +00:00Commented Oct 22, 2012 at 23:37
2 Answers
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).