在 VC 2015 上使用带有字符串的宏失败

2023-09-27C/C++开发问题
1

本文介绍了在 VC 2015 上使用带有字符串的宏失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为什么编译失败?

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 上使用带有字符串的宏失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6