8

Does C++17 offer any way to load/read a file at compile time? More specifically, I want to load a file's content into a const char* or std::string_view, etc. For example:

    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);


I've come across this answer Is it possible to read a file at compile time? from 2014. The solution by teivaz works perfectly, using some macro & #include magic. However, it requires the input file to be changed: the file should enclose its content in STR( ... ), which may not always be possible.

Since the above answer is quite old, I thought maybe things have changed, and perhaps C++17 (or possibly C++20) offers some functionality to overcome this issue.

Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
  • Beyond `#include` there's no way to read files at compile-time. Reading generic files needs calls to the operating system which just can't be done at compile-time. – Some programmer dude Jun 13 '19 at 09:22
  • Why don't you simply copy the content of the file into your source file if you need to have that statically? – vahancho Jun 13 '19 at 09:24
  • Also, ***why*** do you want to do this? What is the *real* problem you need to solve? At the moment, this post is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Jun 13 '19 at 09:27
  • Oh and depending on your platform, there are non-standard workarounds... If, for example, you're building on Windows then you could create a *resource file* which can include arbitrary data and files as compile-time. Then you could use Windows-specific functions to read that data out from the resource at run-time. – Some programmer dude Jun 13 '19 at 09:29
  • @Someprogrammerdude I disagree. My question is about the existence of a feature/how to do something. Just like so many other questions, e.g. this question https://stackoverflow.com/questions/1135841/c-multiline-string-literal is important and also has many good answer, which is exactly what so many people use SE for. – Phil-ZXX Jun 13 '19 at 09:40
  • If we know the underlying problem you try to solve, then we could help you find possible other solutions. If you ask for help with an existing solution to an unknown problem, you limit the possibilities. – Some programmer dude Jun 13 '19 at 09:41
  • 1
    @Someprogrammerdude I appreciate your help. I assumed it would help others too, if my question was kept generic. As for my use-case, I am trying to store/embed a couple of json files (configs & metadata) in my library so that I don't have to ship them separately with my lib file. That is, the default versions are embedded at compile time. Should the user want to use other metadata, then they can override it at run time with their own (this scenario is unlikely in my case though). Edit: yes, i could copy-paste the jsons manually into a source file, but that doesn't seem viable in the long run. – Phil-ZXX Jun 13 '19 at 10:16

1 Answers1

16

C++20 might ship with std::embed. Have a look at P1040. If this lands, you can

constexpr std::span<const std::byte> someBytes = std::embed("pathToFile");
lubgr
  • 37,368
  • 3
  • 66
  • 117