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

后端 未结 1 560
生来不讨喜
生来不讨喜 2020-12-19 12:55

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

Regardless of using CMake or not, the follow

相关标签:
1条回答
  • 2020-12-19 13:07

    It seems like not possible in standard C++

    Problem 0: Only standard way of textual inclusion is #include directive.

    Problem 1: String literal is a preprocessing token, which are recognised in phase 3, so when preprocessing directives are executed in phase 4, it is already determined that #include is a part of string literal and not a preprocessing directive.

    preprocessing-token:
        header-name
        identifier
        pp-number
        character-literal
        user-defined-character-literal
        string-literal
        user-defined-string-literal
        preprocessing-op-or-punc
        each non-white-space character that cannot be one of the above

    Problem 2: It is impossible to bring preprocessing directive in source and execute it by macro substitution:

    16.3.4/3
    The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one

    So you cannot have working #include inside macro.

    Problem 3: macro replacement list should be a valid preprocessing token:

    control-line:
        # define identifier replacement-list new-line
    replacement-list:
            pp-tokens opt
    pp-tokens:
        preprocessing-token
        pp-tokens preprocessing-token

    And string literal is a preprocessing token itself, you cannot build string literal from several macro.

    0 讨论(0)
提交回复
热议问题