在初始化 constexpr(字符串?)时使用#include

Using #include when initializing a constexpr (string ?)

我遇到了这段代码,但我不知道它是如何工作的——我从未见过这样使用 include:

static constexpr auto VAR_NAME = 
    #include "path/to/file/FileName"
;

文件path/to/file/FileName包含一些字符串的内容,即

R"(
contents of string
)"

有人可以解释一下这段代码的作用吗?

这正是它所说的:the contents of the named file are "included" into your source code

这可以在源代码的任何行上完成。它只是从字面上粘贴文件内容。因此,生成的代码是:

static constexpr auto VAR_NAME = 
R"(
contents of string
)"
;

据推测,将字符串文字保存在单独的文件中对原作者有用,可能是为了使本地化更容易,也可能只是为了“整洁”。

#include 没有 用于头文件(尽管这是正常的应用程序)。

它做了 include 所做的事情,只是包含 include 指令所在的文件的内容 'called'。

#include 用文件的内容替换该行。

include 有两种变体,使用 <> 或使用 ""(即:#include "file"), 第一个在系统范围内的公共位置搜索文件,第二个在本地指定位置搜索文件(即:在编译器标志中指定)