玩弄,我注意到以下代码在 MSVC 19.27 上编译
template <typename T>
concept defined = true;
template <!defined T> // <=== !!!!!!!!
inline auto constexpr Get()
{
return 5;
}
这是怎么回事?允许这种语法是一个坏主意吗?
最佳答案
你是对的; MSVC 19.27 和 19.28(VS16.9 之前)支持 !
的语法用于否定概念(参见 compiler explorer )。
即使 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
关于c++ - 否定一个概念 (C++20),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63435642/