C++ 在#include 指令中使用 ..(点)是否合法?
C++ Is it legal to use .. (dot dot) in #include directive?
那里。正如标题所说。
使用#include"test.h"
是合法的。
使用#include"test/test.h"
也是合法的。
但是使用#include"../test.h"
合法吗?
例如,#pragma once
几乎在所有编译器中都是合法的。
但这在 C++ 中不是标准的。
我找不到任何文件说 .. "guarantee" 是 parent 目录的意思。
谁能帮帮我?
C++ 标准在 §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.
就是这样:全部 implementation-defined。 §16.2 然后说,除此之外,还有一些与您的问题没有直接关系的事情:
A preprocessing directive of the form
#include
"
q-char-sequence"
new-line
causes 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.
这很有道理。为什么不能为不使用“..”约定的文件系统创建 C++ 编译器?
总而言之,回答您的问题:是的,根据 C++ 标准,它是合法的,但这并不奇怪,因为对于 #include
指令,几乎所有内容在技术上都是合法的。
更有趣的问题是您的 C++ 实现的文档对此有何评论。但即便如此,您可能也会对这个主题留下一个相当笼统的解释。例如,MSVC 2015 documentation for #include
表示:
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.
那里。正如标题所说。
使用#include"test.h"
是合法的。
使用#include"test/test.h"
也是合法的。
但是使用#include"../test.h"
合法吗?
例如,#pragma once
几乎在所有编译器中都是合法的。
但这在 C++ 中不是标准的。
我找不到任何文件说 .. "guarantee" 是 parent 目录的意思。
谁能帮帮我?
C++ 标准在 §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.
就是这样:全部 implementation-defined。 §16.2 然后说,除此之外,还有一些与您的问题没有直接关系的事情:
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.
这很有道理。为什么不能为不使用“..”约定的文件系统创建 C++ 编译器?
总而言之,回答您的问题:是的,根据 C++ 标准,它是合法的,但这并不奇怪,因为对于 #include
指令,几乎所有内容在技术上都是合法的。
更有趣的问题是您的 C++ 实现的文档对此有何评论。但即便如此,您可能也会对这个主题留下一个相当笼统的解释。例如,MSVC 2015 documentation for #include
表示:
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.