【发布时间】:2020-12-27 11:23:24
【问题描述】:
我有一些代码试图使用一个概念来指定对类的成员函数的要求:
#include <type_traits>
template <typename A>
concept MyConcept = requires(A a, bool b) {
{ a.one() } -> bool;
a.two();
a.three(b);
};
不幸的是,clang 10.0.0 在https://godbolt.org 上使用-std=c++20 会产生错误:
<source>:5:18: error: expected concept name with optional arguments [clang-diagnostic-error]
{ a.one() } -> bool;
^
有人知道 clang 所期望的语法吗?我已经尝试了许多基于来自各种来源的样本的变体,例如 Compound Requirements sample,但到目前为止还没有运气:
#include <type_traits>
template<typename T> concept C2 =
requires(T x) {
{*x} -> std::convertible_to<typename T::inner>; // the expression *x must be valid
// AND the type T::inner must be valid
// AND the result of *x must be convertible to T::inner
{x + 1} -> std::same_as<int>; // the expression x + 1 must be valid
// AND std::same_as<decltype((x + 1)), int> must be satisfied
// i.e., (x + 1) must be a prvalue of type int
{x * 1} -> std::convertible_to<T>; // the expression x * 1 must be valid
// AND its result must be convertible to T
};
任何帮助表示赞赏。
【问题讨论】:
-
似乎它需要右侧的元函数:
{ a.one() } -> std::same_as<bool>;。正如例子所说。 :-) -
是的,前段时间提案中的语法已更改。
-
当我使用带有 -std=c++20 的 x86-64 clang 10.0.0 在 Godbolt 上运行示例时,使用元函数的示例也给了我
expected concept name with optional arguments错误。
标签: c++ templates c++20 c++-concepts