问题描述
为什么编译失败?
char programDate[] = "("__DATE__")";
但这编译得很好(见空间):
But this compiles fine (see space):
char programDate[] = "(" __DATE__")";
我知道 VC2015 现在支持文字运算符.但这不应该在编译阶段吗?__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?
我想到了一些 Unicode/非 Unicode 构建的混合匹配问题 - 但它没有帮助.这不仅是预定义宏的问题,还有用户定义的问题:
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")";
Error C3688 invalid literal suffix '__DATE__'; literal operator or literal operator template 'operator ""__DATE__' not found
推荐答案
从 C++11 开始,用户定义的文字就存在并且是预处理的一部分.语法是:
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
所以 "("__DATE__
匹配 preprocessing-token,但 "("
__DATE__
不匹配 (那是两个单独的预处理标记).
So "("__DATE__
matches preprocessing-token, but "("
__DATE__
doesn't (that is two separate preprocessing tokens).
宏替换发生在标记化之后.由于您的第一个示例中没有标记 __DATE__
,因此没有替换.
Macro replacement happens after tokenization. Since there is no token __DATE__
in your first example, there is no replacement.
这篇关于在 VC 2015 上使用带有字符串的宏失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!