Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using macro with string fails on VC 2015

Why does this fail to compile?

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

But this compiles fine (see space):

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

I do know VC2015 now supports literal-operators. But shouldn't that be in compilation phase? __DATE__ should have been processed by the pre-processor. What is going on here?

I thought of some mix-match issue with Unicode/non-Unicode build - but it doesn't help. It's not just issue with pre-defined macros, but with user defined also:

#define MACRO "abc"
char data[] = "("MACRO")";

EDIT:

Error C3688 invalid literal suffix '__DATE__'; literal operator or literal operator template 'operator ""__DATE__' not found
like image 259
Ajay Avatar asked Jul 31 '15 05:07

Ajay


1 Answers

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.

like image 50
M.M Avatar answered Nov 03 '22 01:11

M.M