Negating a concept (C++20)

Viewed 1070

Playing around, I noticed that the following code compiles on MSVC 19.27

template <typename T>
concept defined = true;

template <!defined T>             // <=== !!!!!!!!
inline auto constexpr Get()
{
    return 5;  
}

What's going on? Is it such a bad idea to allow this syntax?

2 Answers

No, you are not allowed to apply operators to concepts when they are used as part of placeholder or terse-template syntax. If you need to do that, then you need to either create a new concept or spell it out long-form with a requires clause.

You're right; MSVC 19.27 and 19.28 (before VS16.9) support syntax with ! for negation of concept (cf in compiler explorer).

Even if this syntax is not allowed in C++20, you can do something very close

template<typename T>
concept defined = your_rule_on<T>;

template <typename T>
requires defined<T>
inline auto constexpr Get() { /* ... */ }

template <typename T>
requires(!defined<T>) // <=== !
inline auto constexpr Get() { /* ... */ }

demo

Related