【发布时间】:2021-05-27 09:41:08
【问题描述】:
我尝试使用 requires 表达式定义一个概念,该表达式检查该类型上是否存在某些成员函数。
我遇到的问题是,必须插入一个概念以进行返回类型检查,而无需任何前面的 bool 操作,如“not”。如果需要,这迫使我还要编写逆的概念,这不是很方便。
有什么我遗漏的东西或其他好方法吗?
简单示例:
template <typename T>
concept IsReference = std::is_reference_v<T>;
template <typename T>
concept HasSomeFunction = requires (T t) {
{t.func()} -> IsReference; /* this is ok */
{t.anotherFunc()} -> (not IsReference); /* this is not ok */
};
【问题讨论】:
-
所以……您可以使用
void、volatile void*、wchar_t(*(*(*)[])(...))[1]、float std::vector<bool>::*、std::in_place_type_t<void() volatile &&>、std::pair<int&,char&>和std::reference_wrapper<int>,但不能使用const int&? “除此之外的任何东西”的语法似乎都有问题。
标签: c++ templates require c++-concepts