1

when we was programming C file extensions were usually "C" or "CC" or "H" based. then we migrated to C++ and new file extensions were introduced "cpp" and "hpp" although "hpp" is not commonly used (or at least I think so), so what is new C++0x file extensions or how do they differ from c/c++ files?

sehe
  • 374,641
  • 47
  • 450
  • 633
Ali1S232
  • 3,373
  • 2
  • 27
  • 46
  • google to the rescue! http://en.wikipedia.org/wiki/C%2B%2B0x – Fredrik Pihl Jun 19 '11 at 20:16
  • 1
    I'd imagine it will be the same. C++0x is backwards compatible with previous standards. There is no reason to distinguish it, and extensions are merely for your convenience. – someguy Jun 19 '11 at 20:17
  • @someguy, c++ was also compatible with C, I mean almost every C code can be compiled using a c++ compiler! why did they change extention then? it was to define there may be some things that "C" compiler doesn't understand. the same goes for C++0x – Ali1S232 Jun 19 '11 at 20:21
  • 1
    @someguy: C++0x is only backwards compatible to [a certain extent](http://stackoverflow.com/q/6399615/46642). – R. Martinho Fernandes Jun 19 '11 at 20:32
  • I could see individual organizations distinguishing "legacy" C++ code that has to be compiled without the Ox additions. But long term, I don't see a push to change extensions taking root. – Dennis Zickefoose Jun 19 '11 at 21:08

6 Answers6

17

C++0x is just a new standard for C++, not a new language. There's no need to change the file extension and I don't believe anyone will.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • C++0x may not differ to c++ as much as c++ differ to C, but there are still too many diffrences, at least I can say c++0x file can't be compiled with a normal c++ compiler (just like c++ file can't be compiled with C compiler) and they are both backward compatible. I mean c++0x compiler can compile c++ or C code and c++ can understand what's written inside C file! the only possible thing that may prevent creating a new extention, is to completely obsolute pure c++ compilers (something they didn't to C compilers) – Ali1S232 Jun 19 '11 at 20:31
  • 3
    Once the standard is finished and approved by ISO, there will be no such thing as C++0x compiler. It will either comply to latest standard or not. Do we distinguish between C++98 and C++03 compilers, did the filename extension get changed between C++98 and 03? Or even C90 and C99? – Vitus Jun 19 '11 at 20:46
  • @Vitus: I distinguish between C90 and C99 *compilers*, and so does GNU, which writes one (of each). Just not between C90 and C99 source by filename. The reason not to distinguish between C++98 and C++03 compilers, I believe, is that any serious, actively-maintained, standards-conforming compiler migrated from C++98 to C++03, and I think most abandoned the former entirely. There were a very few compatibility breaks, but I guess not enough to create demand for legacy support. That won't be the case with C++0x, at least not for some considerable time. – Steve Jessop Jun 19 '11 at 21:46
  • @Gajet Microsoft doesn't even provide a compiler switch for VS2010. They accept C or a subset of C++0x. They make no distinction between C++11 and C++03. And I imagine that once clang and GCC have well-tested C++11 compiling, they will do the same. Those two will probably have flags for backwards-compatibility C++03 mode, but the default will eventually become C++11. Because it's the new version of the standard. – Nicol Bolas Jun 19 '11 at 21:46
3

For me there is no reason to change the file extension. C++0x is always C++, not another language. C and C++ are not the same language, so it's normal to have another extensions. But in that case, I see no reason to change the extension.

Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
  • C++0x may not differ to c++ as much as c++ differ to C, but there are still too many diffrences, at least I can say c++0x file can't be compiled with a normal c++ compiler (just like c++ file can't be compiled with C compiler) and they are both backward compatible. I mean c++0x compiler can compile c++ or C code and c++ can understand what's written inside C file! the only possible thing that may prevent creating a new extention, is to completely obsolute pure c++ compilers (something they didn't to C compilers) – Ali1S232 Jun 19 '11 at 20:25
3

The language doesn't specify anything about the file extensions, it doesn't care. It's entirely down to the implementation.

Compilers that I've used do not use file extensions to distinguish between different dialects of C++. Neither for that matter do they use file extensions to distinguish between C89 and C99 source. For example in g++, the way to specify dialect is with the -std, -pedantic, -ansi command line options.

As far as I know, this policy will continue: once C++0x becomes a standard and g++ declares it supported, the way to specify whether you want C++03 or C++11[*] will be with the -std option.

You can of course invent your own convention (just as you currently choose between .cc, .cpp, .C, .cxx or whatever other file extensions different people have used for C++). If you feel that file extension is the way to go, and you use make, define a rule:

*.o : *.cpp11
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -std=c++11 -c

The details for other build systems and other compilers of course will vary, but it's all beyond the scope of the standard, which just defines the language, not what files it's stored in.

If you're concerned about a file that uses C++0x accidentally being compiled as C++03, then you have a couple of options. If it uses C++0x features that are absent from C++03 then often the file won't compile as C++03, in which case you don't need to do anything (other than fix your build options). Otherwise you can deliberately ensure that it uses such a feature, for example to ensure a version of C++ at least as recent as the FDIS:

#if __cplusplus < 201103L
    #error "Wrong version!"
#endif

[*] let's hope.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

You can save c++ files as .c and headers files as .h. The compiler won't really care. It's just a new standard, not a new language.

pikzen
  • 1,413
  • 1
  • 10
  • 18
  • 2
    Saying “the compiler won’t really care” isn’t correct either. The file extension triggers a lot of different handling on the compilers so that `.c` and `.cpp` files (say) may be handled differently. – Konrad Rudolph Jun 19 '11 at 20:53
2

Why should there be a new extension? The "new" C++0x will be a simple, plain "old" C++ once it's finished. No need for a new extenion or anything... It won't even be called "C++0x" or "C++11" anymore, only if it's really important to differentiate between the standards.

Xeo
  • 129,499
  • 52
  • 291
  • 397
0

You can use whatever extensions you feel like using. No change there.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490