【发布时间】:2021-11-01 12:37:44
【问题描述】:
背景:我正在尝试在 MSVC 项目中重新利用为 GCC 编写的一些 C++ 代码。我一直在尝试重构代码以使其与 MSVC 编译器兼容。
简化,原来的功能之一是这样的:
[[nodiscard]] constexpr int count() const noexcept {
return __builtin_popcountll(mask);//gcc-specific function
}
其中 mask 是 64 位成员变量。向 MSVC 的明显转换是:
[[nodiscard]] constexpr int count() const noexcept {
return __popcnt64(mask); // MSVC replacement
}
但是,它无法编译,因为 __popcnt64 不允许在 constexpr 函数中使用。
我正在使用 C++17,如果可能,我希望避免切换到 C++20。
有没有办法让它工作?
【问题讨论】:
标签: c++ visual-c++ c++17 bit-manipulation constexpr