为什么 Clang 不允许“和”作为函数名?

Run*_*nik 4 c++ clang clangpowertools

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

以下示例工作正常:

class C{};

inline void andi(C& s);

int main()
{
    std::cout << "Hello, world!\n";
}
Run Code Online (Sandbox Code Playgroud)

这个没有:

class C{};

inline void and(C& s);

int main()
{
    std::cout << "Hello, world!\n";
}
Run Code Online (Sandbox Code Playgroud)

我在 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);
Run Code Online (Sandbox Code Playgroud)

看起来以二元运算命名的函数(如和、非或和异或)在编译器中触发了错误的行为。使用 Visual Studio 编译器未显示任何错误,程序按预期工作。

我能做些什么来防止这种情况发生吗?或者这是 Clang 中的错误?将 NOLINT 添加到该行无济于事,因为它是引发错误的编译器......

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

谢谢 !

pab*_*285 5

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

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