问题描述
为什么无法编译?
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上将宏与字符串一起使用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!