【发布时间】:2018-06-24 16:28:59
【问题描述】:
我写了一个这样的is_incrementable trait:
#include <type_traits>
template <typename T, typename = void>
struct is_incrementable : std::false_type {};
template <typename T>
struct is_incrementable<T, std::void_t<decltype(++std::declval<T&>())>>
: std::true_type {};
template <typename T>
constexpr bool is_incrementable_v = is_incrementable<T>::value;
当我将它应用到bool 和-std=c++17 时,它返回true:
// This compiles
static_assert(is_incrementable_v<bool>, "");
但在 c++17 下不允许递增 bool。事实上,如果我尝试这样做,我会得到一个错误:
bool b = false;
++b;
结果:
error: ISO C++17 does not allow incrementing expression of type bool [-Wincrement-bool]
为什么 SFINAE 报告 bool 是可递增的,而编译器显然不允许这样做?
编译器资源管理器:https://godbolt.org/g/DDFYBf
【问题讨论】:
-
它确实在 GCC 中给出了预期的静态断言失败,这至少暗示它可能只是 clang 中的一个错误。
-
@max66 因为
operator ++需要一个左值 -
是的,开始认为这是一个编译器错误。 GCC 似乎确实做了正确的事情。
-
是的,这似乎是一个错误。如果你从 declval 中删除引用,它对我有用。
标签: c++ sfinae typetraits