15

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
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
Ajay
  • 18,086
  • 12
  • 59
  • 105

1 Answers1

24

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 1
    In case anyone didn't realize the C++ preprocessor differs from the C preprocessor... now you do :) – M.M Jul 31 '15 at 05:51
  • 5
    With C++11 user-defined literals in VS 2015, you can also run into problems with ``L"Hello, "L"World"`` would work before but fails with the same error because it tries to use the second L as a UDF. You fix it with whitespace: ``L"Hello, " L"World"`` – Chuck Walbourn Jul 31 '15 at 06:18
  • And thankfully now mismatched strings can be concatenated - so second `L` isn't needed! (http://stackoverflow.com/questions/31809141/concatenating-mismatched-string-works-in-vc2015-how) – Ajay Aug 05 '15 at 06:37
  • Was it changed because it conflicts with postfix string operators `"Hello world"s` ? http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s – Sandburg Apr 25 '18 at 13:03