pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.73k stars 2.11k forks source link

[BUG]: Build failed on MSVC2022 when using not/and/or as boolean operator #4010

Closed LittlePox closed 2 years ago

LittlePox commented 2 years ago

Required prerequisites

Problem description

Cpp code using not/and/or as boolean operator will get compilation error. Using !/&&/|| is fine.

The below code complains as:

"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\user\AppData\Local\Programs\Python\Python38\Lib\site-packages\pybind11\include "-IC:\Program Files\Python38\Lib\site-packages\pybind11\include" "-IC:\Program Files\Python38\include" "-IC:\Program Files\Python38\Include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt" /EHsc /Tpexample.cpp /Fobuild\temp.win-amd64-3.8\Release\example.obj /O2 /std:c++17 example.cpp example.cpp(4): error C2146: syntax error: missing“;”(before identifier “and”) example.cpp(4): error C2065: “and”: undeclared identifier example.cpp(4): error C2146: syntax error: missing“;”(after identifier “b”) error: command 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x64\cl.exe' failed with exit code 2

Reproducible example code

#include <pybind11/pybind11.h>

bool and_bool(bool a, bool b) {
    return a and b;
}

PYBIND11_MODULE(example, m) {
    m.def("and_bool", &and_bool, "and_bool");
}
LittlePox commented 2 years ago

Opps never mind. adding -fpermissive and it compiles.