gpt4 book ai didi

c++ - 为什么 Clang 不允许 "and"作为函数名称?

转载 作者:行者123 更新时间:2023-12-02 22:26:59 26 4
gpt4 key购买 nike

我正在使用 Visual Studio 2017 开发一个程序有一段时间了。最近我安装了 Clang Power Tool 扩展以检查代码的质量。我的程序的一部分是模拟 CPU 的操作码。我在下面创建了一个精简的示例。

以下示例运行良好:

class C{};

inline void andi(C& s);

int main()
{
std::cout << "Hello, world!\n";
}

这个没有:

class C{};

inline void and(C& s);

int main()
{
std::cout << "Hello, world!\n";
}

我在 Clang 3.8.0 上遇到这些错误(我在程序中使用版本 9.0.1,错误类似):

source_file.cpp:9:18: error: expected ')'
inline void and(C& s);
^
source_file.cpp:9:16: note: to match this '('
inline void and(C& s);
^
source_file.cpp:9:13: error: cannot form a reference to 'void'
inline void and(C& s);
^
source_file.cpp:9:1: error: 'inline' can only appear on functions
inline void and(C& s);

看起来以二进制运算(例如 and、not、or 和 xor)命名的函数会在编译器中触发错误行为。使用 Visual Studio 编译器不会显示任何错误,并且程序按预期工作。

我可以做些什么来防止这种情况发生吗?或者这是 Clang 中的一个错误?将 NOLINT 添加到该行并没有帮助,因为编译器会引发错误......

您可以在这里测试该案例:https://rextester.com/TXU19618

谢谢!

最佳答案

and 是 C++ 中的保留关键字,这意味着它不能用于函数名称。这是标准 C++ 行为,而不是错误。

https://en.cppreference.com/w/cpp/keyword

关于c++ - 为什么 Clang 不允许 "and"作为函数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60317979/

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