繁体   English   中英

clang 10 C++20 概念如何指定类方法的复合要求?

[英]How can a clang 10 C++20 concept specify compound requirements for class methods?

我有一些代码试图使用一个概念来指定对类成员函数的要求:

#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 编译得很好(尽管如果你没有 stdlib,你可能需要自己提供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());
}

暂无
暂无

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

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