最終更新日時(UTC):
が更新

履歴 編集

concept
<concepts>

std::relation(C++20)

namespace std {
  template<class R, class T, class U>
  concept relation =
    predicate<R, T, T> && predicate<R, U, U> &&
    predicate<R, T, U> && predicate<R, U, T>;
}

概要

relationは、任意の述語Rが型T, Uの間の二項関係にあることを表すコンセプトである。

#include <iostream>
#include <concepts>

template<typename R, typename T, typename U>
requires std::relation<R, T, U>
void f(const char* name, const char* tname, const char* uname) {
  std::cout << name << " is relation between " << tname << " and " << uname << std::endl;
}

template<typename R, typename T, typename U>
void f(const char* name, const char* tname, const char* uname) {
  std::cout << name << " is not relation between " << tname << " and " << uname << std::endl;
}


struct S1 {
  int n = 0;
};

struct S2 {
  short m = 0;
};

// S1とS2の間の同値関係
struct relation_s1s2 {

  bool operator()(S1 lhs, S1 rhs) const {
    return lhs.n == rhs.n;
  }

  bool operator()(S2 lhs, S2 rhs ) const {
    return lhs.m == rhs.m;
  }

  bool operator()(S1 lhs, S2 rhs) const {
    return lhs.n == rhs.m;
  }

  bool operator()(S2 lhs, S1 rhs) const {
    return lhs.m == rhs.n;
  }
};

// 単にS1とS2の間の述語であるだけではrelationではない
struct not_relation_s1s2 {

  bool operator()(S1 lhs, S2 rhs) const {
    return lhs.n == rhs.m;
  }

  bool operator()(S2 lhs, S1 rhs) const {
    return lhs.m == rhs.n;
  }
};

int main() {
  f<relation_s1s2, S1, S2>("relation_s1s2", "S1", "S2");
  f<not_relation_s1s2, S1, S2>("not_relation_s1s2", "S1", "S2");
}

出力

relation_s1s2 is relation between S1 and S2
not_relation_s1s2 is not relation between S1 and S2

バージョン

言語

  • C++20

処理系

関連項目

参照