簡體   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