gpt4 book ai didi

c++ - 为什么 clang 将其解析为用户定义的文字?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:50 24 4
gpt4 key购买 nike

我有一些正在维护的代码,我已经开始在 clang 3.3 下编译这些代码。使用“-std=c++11”编译时,clang 会生成错误(如下所示)。我已将有问题的代码提炼为以下内容:

#include <stdio.h>

#define DBG_PRT(__format, ...) \
printf("%s:%d:%s: "__format, __FILE__, \
__LINE__, __FUNCTION__, ## __VA_ARGS__)

int main()
{
DBG_PRT("%s\n", "Hi");
}

这是 clang 的输出:

test.cpp:10:5: error: no matching literal operator for call to 'operator "" __format' with arguments of types 'const char *' and 'unsigned int'

DBG_PRT("%s\n", "Hi");

^ test.cpp:4:29: note: expanded from macro 'DBG_PRT'
printf("%s:%d:%s: "__format, __FILE__, \
^ 1 error generated.

如果字符串文字和“__format”之间没有空格,预处理器似乎不能扩展 __format。不过,当未指定 -std=c++11 时,它显然。 G++ 4.4.7(带和不带 -std=c++0x)编译都很好。

编译器是否有错误?

最佳答案

这是因为 ""_ 是用户定义的字符串文字的语法。在两者之间放置一个空格以具有旧行为(连接文字)。 GCC 工作正常,因为 4.4.7 没有实现用户定义的文字 (it appeared in version 4.7)。

此外,正如@Fred 所指出的,尽量避免使用保留标识符(双下划线)。

关于c++ - 为什么 clang 将其解析为用户定义的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18068006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com