Difference between using #include and #include in C++

前端 未结 5 1008
深忆病人
深忆病人 2020-11-29 23:16

What is the difference between using #include and #include> in C++? Which of the two is used and why is it is used?

5条回答
  •  旧巷少年郎
    2020-11-29 23:59

    C++ only include-files not found in the C standard never used filename.h . Since the very first C++ Standard came out (1998) they have used filename for their own headers.

    Files inherited by the C Standard became cfilename instead of filename.h. The C files inherited used like filename.h are deprecated, but still part of the C++ standard.

    The difference is that names not defined as macros in C are found within namespace std:: in cfilename in C++, while names in filename.h are within the global namespace scope. So you will find ::size_t in stddef.h, and std::size_t in cstddef. Both are Standard C++, but use of ::size_t is deprecated (See Annex D of the C++ Standard).

    Now those were the difference.

    Why would you use `filename.h` ?

    • Compatibility with C compilers
    • Compatibility with very old C++ compilers

    Why should you use `cfilename` ?

    • Names are within namespace std:: . No name-clashes anymore.
    • New C++ features (e.g. overloaded math functions for float, long)
    • C Compatibility Headers (filename.h) could disappear in future.

提交回复
热议问题