Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negating a concept (C++20)

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?

like image 832
non-user38741 Avatar asked Apr 26 '26 05:04

non-user38741


1 Answers

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

like image 131
Pascal H. Avatar answered Apr 27 '26 21:04

Pascal H.