简体   繁体   English

否定一个概念 (C++20)

[英]Negating a concept (C++20)

Playing around, I noticed that the following code compiles on MSVC 19.27玩耍时,我注意到以下代码在 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?允许这种语法是一个坏主意吗?

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.如果您需要这样做,那么您需要创建一个新概念或使用requires子句将其拼写为长格式。

You're right;你是对的; MSVC 19.27 and 19.28 (before VS16.9) support syntax with ! MSVC 19.27 和 19.28(VS16.9 之前)支持带有!的语法for negation of concept (cf in compiler explorer ).用于否定概念(参见编译器资源管理器)。

Even if this syntax is not allowed in C++20, you can do something very close即使 C++20 中不允许这种语法,你也可以做一些非常接近的事情

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演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM