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

前提是你 提交于 2019-11-29 11:07:58

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!