Why does std::visit in an unsatisfied concept cause a compile error in gcc

  c++, c++-concepts, c++20, gcc

This code:

#include <concepts>
#include <string>
#include <variant>

struct any_callable {
public:
    template<typename T>
    void operator()(T&&) {}
};

template<typename V>
concept is_variant = requires(V v) { std::visit(any_callable{}, v); };

int main() {
    constexpr bool wrapped = is_variant<std::string>;
}

does not compile under gcc 11. It gives a bunch of errors about valueless_by_exception and such. It does, however, compile under msvc (godbolt). Now as I understand concepts, in this instance, if it would usually fail to compile it will decay to false, otherwise to true. The behaviour of msvc would seem to support this.

So: Is this a bug in gcc, a non-standard feature of msvc, and/or is my code wrong?

Source: Windows Questions C++

LEAVE A COMMENT