Using macro with string fails on VC 2015

后端 未结 1 1763
Happy的楠姐
Happy的楠姐 2020-12-10 12:15

Why does this fail to compile?

char programDate[] = \"(\"__DATE__\")\";

But this compiles fine (see space):

char programDat         


        
相关标签:
1条回答
  • 2020-12-10 12:42

    Since C++11, user-defined literals exist and are part of preprocessing. The grammar is:

    preprocessing-token:
        user-defined-string-literal
        // other stuff...
    
    user-defined-string-literal:
        string_literal ud-suffix
    
    ud-suffix:
        identifier
    

    So "("__DATE__ matches preprocessing-token, but "(" __DATE__ doesn't (that is two separate preprocessing tokens).

    Macro replacement happens after tokenization. Since there is no token __DATE__ in your first example, there is no replacement.

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