There are two path separators in common use: the Unix forward-slash and the DOS backslash. Rest in peace, Classic Mac colon. If used in an #include directive, are they equal under the rules of the C++11, C++03, and C99 standards?
C++ – Is the backslash acceptable in C and C++ #include directives
cc-preprocessorheader-filesinclude
Related Topic
- C++ – What are the differences between a pointer variable and a reference variable in C++
- C++ – What does the explicit keyword mean
- C++ – How to iterate over the words of a string
- C++ – The Definitive C++ Book Guide and List
- Do I cast the result of malloc
- Javascript – How to include a JavaScript file in another JavaScript file
- C++ – the “–>” operator in C/C++
- Php – Difference between require, include, require_once and include_once
Best Answer
C99 says (§6.4.7/3):
C++03 says (§2.8/2):
C++11 says (§2.9/2):
Therefore, although any compiler might choose to support a backslash in a
#include
path, it is unlikely that any compiler vendor won't support forward slash, and backslashes are likely to trip some implementations up by virtue of forming escape codes. (Edit: apparently MSVC previously required backslash. Perhaps others on DOS-derived platforms were similar. Hmmm… what can I say.)C++11 seems to loosen the rules, but "conditionally supported" is not meaningfully better than "causes undefined behavior." The change does more to reflect the existence of certain popular compilers than to describe a portable standard.
Of course, nothing in any of these standards says that there is such a thing as paths. There are filesystems out there with no paths at all! However, many libraries assume pathnames, including POSIX and Boost, so it is reasonable to want a portable way to refer to files within subdirectories.