there. As title saying.
Use #include"test.h"
is legal.
Use #include"test/test.h"
is also legal.
But is it legal to use #include"../test.h"
?
For example, #pragma once
is legal in almost every compilers.
But this is not standard in C++.
I cannot find any documents which saying .. "guarantee" to be meaning of parent directory.
Can anyone help me?
The C++ standard says in §2.8/1:
Header name preprocessing tokens shall only appear within a
#include
preprocessing directive (16.2). The sequences in both forms of header-names are mapped in an implementation-defined manner to headers or to external source file names as specified in 16.2.
So here you have it: It's all implementation-defined. §16.2 then says, among some other things not directly related to your question:
A preprocessing directive of the form
#include
"
q-char-sequence"
new-linecauses the replacement of that directive by the entire contents of the source file identified by the specified sequence between the
"
delimiters. The named source file is searched for in an implementation-defined manner.
And this makes a lot of sense. Why shouldn't you be able to create a C++ compiler for file systems that do not use the ".." convention?
In summary, to answer your question: Yes, it is legal according to the C++ standard, but that's not surprising because almost everything is technically legal for an #include
directive.
The more interesting question is what the documentation of your C++ implementation has to say about this. But even then you may be left with a rather generic explanation on the topic. For example, the MSVC 2015 documentation for #include
says:
The path-spec is a file name that may optionally be preceded by a directory specification. The file name must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.