我需要一个函数将"显式"转义序列转换为相对不可打印的字符.ES:
char str[] = "\\n";
cout << "Line1" << convert_esc(str) << "Line2" << endl:
Run Code Online (Sandbox Code Playgroud)
会给出这个输出:
Line1
Line2
Run Code Online (Sandbox Code Playgroud)
有没有这样做的功能?
我刚刚注意到我为这个问题给出的答案实际上不起作用:
无论是否使用 CMake,以下内容都适用于当前标准:
Run Code Online (Sandbox Code Playgroud)std::string resource = R"( #include "text.txt" )";
我认为预处理器会首先识别#include "text.txt"语句并扩展文本。
但显然事实并非如此,结果为
std::cout << resource << std::endl;
Run Code Online (Sandbox Code Playgroud)
是
Run Code Online (Sandbox Code Playgroud)#include "text.txt"
我尝试使用一些宏来让#include语句在其中展开,但它也不起作用:
#include <string>
#include <iostream>
#define RESOURCE_DEFINIION(resource_var,resource_name) \
const std::string resource_var = R"xxx( \
#include resource_name \
)xxx";
RESOURCE_DEFINIION(resource,"text.txt")
int main()
{
std::cout << resource << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
Run Code Online (Sandbox Code Playgroud)\ #include resource_name \
是否有任何技巧可以text.txt使用预处理器或任何其他常规 C++ 语言功能将资源提取到 c++-11 原始字符串文字中?
免责声明:
我很清楚上述样本有什么问题,以及为什么会以这种方式失败。预处理器忽略出现在"成对中的东西是一个问题。
那么有没有办法让预处理器看到这些?