vendredi 3 juin 2016

Is there a way to pull in a text resource into a raw string literal using the pre-processor?

I've just noticed that an answer I have given for this question actually doesn't work:

Regardless of using CMake or not, the following should work with the current standard:

std::string resource = R"(
#include "text.txt"
)";

I thought that the pre-processor would recognize the #include "text.txt" statement in first place and expand the text.

But that's obviously not the case, the result for

std::cout << resource std::endl;

is

#include "text.txt"

I tried to use some macro to let the #include statement be expanded within, but it doesn't work either:

#include <string>
#include <iostream>

#define RESOURCE_DEFINIION(resource_var,resource_name) \
    const std::string resource = R"xxx( \
    #include resource_name \
    )xxx";

RESOURCE_DEFINIION(resource,"text.txt")

int main()
{
   std::cout << resource << std::endl; 

   return 0;
}

The output is

\                                                                                                                                                                                          
    #include resource_name \                                                                                                                                                                

Here's the demo to play with


Is there any trickery available to pull in the text.txt resource into a c++-11 raw-string literal, using the pre-processor or any other regular c++ language feature?

Aucun commentaire:

Enregistrer un commentaire