【发布时间】:2019-02-01 18:44:48
【问题描述】:
我正在尝试为编译时整数序列实现一些功能。下面是操作or的实现:
#include <cstdint>
#include <utility>
using UInt32 = std::uint32_t;
template<UInt32... s>
using Sequence = std::integer_sequence<UInt32, s...>;
template<UInt32>
constexpr bool find([[maybe_unused]] Sequence<> a = {}) noexcept {
return false;
}
template<UInt32 x, UInt32 v, UInt32... s>
constexpr bool find([[maybe_unused]] Sequence<v, s...> a = {}) noexcept {
if constexpr (v == x) {
return true;
} else {
return find<x, s...>();
}
}
constexpr auto operator|(Sequence<>, Sequence<>) noexcept {
return Sequence<>{};
}
template<UInt32 v, UInt32... s>
constexpr auto operator|(Sequence<v, s...>, Sequence<>) noexcept {
return Sequence<v, s...>{};
}
template<UInt32... s, UInt32 x, UInt32... t>
constexpr auto operator|(Sequence<s...>, Sequence<x, t...>) noexcept {
if constexpr (find<x, s...>()) {
return Sequence<s...>{} | Sequence<t...>{};
} else {
return Sequence<s..., x>{} | Sequence<t...>{}; // error C2679
}
}
我尝试了几种情况通过VC++(19.16.27026.1)检查程序,很多情况导致编译器错误:
Error C2679 binary '|': no operator found which takes a right-hand operand of type 'std::integer_sequence<uint32_t,4,5>' (or there is no acceptable conversion)
例如:
int main() {
[[maybe_unused]] constexpr auto a = Sequence<>{} | Sequence<>{};
[[maybe_unused]] constexpr auto b = Sequence<>{} | Sequence<3, 4, 5>{};
[[maybe_unused]] constexpr auto c = Sequence<1>{} | Sequence<1, 2>{};
[[maybe_unused]] constexpr auto d = Sequence<1>{} | Sequence<3, 4, 5>{}; // VC++, error C2679
}
我尝试了其他编译器,例如 GCC 8.2.0 和 Clang 7.0.0。他们编译任何没有错误的案例。
我不明白为什么会发生此错误。有什么想法吗?
【问题讨论】:
-
您应该提到,这只会在带有
/std:c++17的 VS 2017 中编译,因为它使用 C++17 功能。也就是说,即使使用/permissive-,我仍然可以看到 C2679。海报使用的是最新的 VS 2017 更新(15.9.5 或更高版本)。
标签: c++ templates visual-c++ c++17 variadic-templates