为什么SFINAE没有给出增加bool的正确结果?

Ala*_*son 6 c++ sfinae type-traits

我写了一个is_incrementable这样的特征:

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

当我把它应用到bool-std=c++17上铛,它返回true:

// This compiles
static_assert(is_incrementable_v<bool>, "");
Run Code Online (Sandbox Code Playgroud)

但是bool在c ++ 17下不允许递增.的确,如果我尝试这样做,我会收到一个错误:

bool b = false;
++b;
Run Code Online (Sandbox Code Playgroud)

结果是:

error: ISO C++17 does not allow incrementing expression of type bool [-Wincrement-bool]
Run Code Online (Sandbox Code Playgroud)

bool当编译器明确不允许时,为什么SFINAE报告可递增?

编译器浏览器:https://godbolt.org/g/DDFYBf

Fed*_*dor 1

看起来 Clang 错误地允许bool在未评估的上下文中增加值。

我们可以使用 C++20 概念简化您的示例:

template<class T>
concept is_incrementable = requires(T t) {
    { ++t };
};

int main() {
    static_assert( is_incrementable<int> );
    static_assert( !is_incrementable<bool> ); // Clang error here
}
Run Code Online (Sandbox Code Playgroud)

该程序在 GCC 和 MSVC 中被接受,但 Clang 在这里显示了相同的错误行为,演示:https: //gcc.godbolt.org/z/YEnKfG8T5

我认为这是 Clang bug,因此提交了 bug 报告:https://bugs.llvm.org/show_bug.cgi ?id=52280