Phi*_*ZXX 1 c++ file-io constexpr c++17
C ++ 17是否提供任何在编译时加载/读取文件的方式?更具体地说,我想将文件的内容加载到const char*或std::string_view等等。例如:
constexpr const char* read_file_content(const char* file_path) {
/* should read in the content of the file and return it */
return "";
}
constexpr const char* file_path = "some/folder/file.json";
constexpr const char* file_content = read_file_content(file_path);
Run Code Online (Sandbox Code Playgroud)
我遇到过这个答案是否可以在编译时读取文件?从2014年开始。teivaz的解决方案使用一些宏和#include魔术完美地工作了。但是,它需要更改输入文件:该文件应将其内容包含在STR(...)中,但这可能并不总是可能的。
由于上述答案很旧,我认为可能情况已经改变,也许C ++ 17(或可能的C ++ 20)提供了一些功能来克服此问题。
C ++ 20可能附带std::embed。看一下P1040。如果降落,您可以
constexpr std::span<const std::byte> someBytes = std::embed("pathToFile");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |