From 269caa8ae18eb33e1286e4c21d434ea07228a619 Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 10 Apr 2024 19:37:02 +0000 Subject: deploy: ae498864bb638e597b589c52ff27237835625281 --- development/c++.html | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) (limited to 'development/c++.html') diff --git a/development/c++.html b/development/c++.html index 0d95860..a868300 100644 --- a/development/c++.html +++ b/development/c++.html @@ -813,6 +813,110 @@ static_assert(!is_valid<B>::value, "is false");

A more detailed description is available in the SO discussion How does void_t work.

+

Example: Concepts since c++20

+
// REQUIRES EXPRESSION
+//   requires { requirement-seq }
+//   requires ( parameter-list ) { requirement-seq }
+//
+// [1] https://en.cppreference.com/w/cpp/language/requires
+// [2] https://en.cppreference.com/w/cpp/language/constraints#Constraints
+//
+// REQUIREMENT CLAUSE
+//   Not the same as a REQUIREMENT EXPRESSIONS, and is used to require
+//   constraints (express concept bounds).
+//
+// [1] https://en.cppreference.com/w/cpp/language/constraints#Requires_clauses
+
+// -- HELPER -------------------------------------------------------------------
+
+template<typename T>
+using Alias = T;
+
+void print(int);
+
+// -- CONCEPTS & REQUIRE EXPRESSIONS -------------------------------------------
+
+// Simple concept from a type trait.
+template<typename T, typename U>
+concept Same = std::is_same<T, U>::value;
+
+// Simple requirement concepts.
+template<typename T>
+concept TraitAddAndPrint = requires(T t, int i) {
+    // Adding T + int must be supported.
+    t + i;
+    // Calling print(T) must be available.
+    print(t);
+};
+
+// Type requirement concepts.
+template<typename T>
+concept TraitTypes = requires(T t) {
+    // T must have a type definition inner.
+    typename T::inner;
+    // Type alias must exist.
+    typename Alias<T>;
+};
+
+// Compound requirement concepts.
+template<typename T>
+concept TraitFns = requires(T t, const T c) {
+    // void T::foo() must exist.
+    { t.foo() };
+    // bool T::bar() const; must exist.
+    { c.bar() } -> Same<bool>;
+    // static void T::stat(); must exist.
+    { T::stat() } -> Same<int>;
+};
+
+// Nested requirement concepts.
+template<typename T>
+concept TraitNested = requires(T t) {
+    // Must satisfy other concepts.
+    requires TraitTypes<T>;
+    requires TraitFns<T>;
+};
+
+// -- REQUIRE EXPRESSIONS ------------------------------------------------------
+
+// Require expressions can be evaluated to booleans.
+template<typename T>
+static constexpr bool IsTraitFns = requires { requires TraitFns<T>; };
+
+// Require expressions can also be used in static assertions.
+static_assert(requires { requires Same<int, int>; });
+static_assert(!requires {
+    typename Alias<int>;
+    requires Same<int, void>;
+});
+
+// -- TESTS --------------------------------------------------------------------
+
+static_assert(requires { requires TraitAddAndPrint<int>; });
+
+struct FnTypeGood {
+    using inner = int;
+};
+struct FnTypeBad {};
+static_assert(requires { requires TraitTypes<FnTypeGood>; });
+static_assert(!requires { requires TraitTypes<FnTypeBad>; });
+
+struct FnGood {
+    void foo();
+    bool bar() const;
+    static int stat();
+};
+struct FnBad {};
+static_assert(requires { requires TraitFns<FnGood>; });
+static_assert(!requires { requires TraitFns<FnBad>; });
+
+struct NestedGood : FnTypeGood, FnGood {};
+struct NestedBad1 : FnGood {};
+struct NestedBad2 : FnTypeGood {};
+static_assert(requires { requires TraitNested<NestedGood>; });
+static_assert(!requires { requires TraitNested<NestedBad1>; });
+static_assert(!requires { requires TraitNested<NestedBad2>; });
+

Template selection with partially / fully specializations.

enum Kind {
     kPrimary,
@@ -886,7 +990,7 @@ int main() {
     // * No specialization matches, take the primary template.
 }
 
-

Example: Perfect forwarding

+

Example: Perfect forwarding

#include <cassert>
 #include <cstdio>
 #include <new>
-- 
cgit v1.2.3