clang 10 C++20 概念如何指定 class 方法的复合要求?
How can a clang 10 C++20 concept specify compound requirements for class methods?
我有一些代码试图使用一个概念来指定对 class:
的成员函数的要求
#include <type_traits>
template <typename A>
concept MyConcept = requires(A a, bool b) {
{ a.one() } -> bool;
a.two();
a.three(b);
};
不幸的是,在 https://godbolt.org 上使用 -std=c++20
的 clang 10.0.0 会产生错误:
<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
};
感谢任何帮助。
概念提案已更改,现在需要使用 std::same_as
。
这与 Clang 10 编译良好(尽管如果您没有标准库,您可能需要自己提供 std::same_as
):
template <typename A>
concept MyConcept = requires(A a, bool b) {
{ a.one() } -> std::same_as<bool>;
a.two();
a.three(b);
};
struct SomeType {
bool one() { return true; }
void two() {}
void three(bool) {}
};
bool foo(MyConcept auto a) {
return a.one();
}
void bar() {
foo(SomeType());
}
我有一些代码试图使用一个概念来指定对 class:
的成员函数的要求#include <type_traits>
template <typename A>
concept MyConcept = requires(A a, bool b) {
{ a.one() } -> bool;
a.two();
a.three(b);
};
不幸的是,在 https://godbolt.org 上使用 -std=c++20
的 clang 10.0.0 会产生错误:
<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
};
感谢任何帮助。
概念提案已更改,现在需要使用 std::same_as
。
这与 Clang 10 编译良好(尽管如果您没有标准库,您可能需要自己提供 std::same_as
):
template <typename A>
concept MyConcept = requires(A a, bool b) {
{ a.one() } -> std::same_as<bool>;
a.two();
a.three(b);
};
struct SomeType {
bool one() { return true; }
void two() {}
void three(bool) {}
};
bool foo(MyConcept auto a) {
return a.one();
}
void bar() {
foo(SomeType());
}