From 16e5ab04800f86002e9bd3f904ec60cf63238f91 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 10 Apr 2024 19:52:28 +0200 Subject: c++: add concept (c++20) example --- src/development/c++/Makefile | 5 +- src/development/c++/concepts-20.cc | 105 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/development/c++/concepts-20.cc (limited to 'src/development/c++') diff --git a/src/development/c++/Makefile b/src/development/c++/Makefile index 622bfd3..2c53e47 100644 --- a/src/development/c++/Makefile +++ b/src/development/c++/Makefile @@ -1,4 +1,4 @@ -SRC = concepts-11.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc tmpl-void_t.cc fwd.cc fwd-perfect.cc +SRC = concepts-11.cc concepts-20.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc tmpl-void_t.cc fwd.cc fwd-perfect.cc BIN = $(SRC:.cc=) all: $(BIN) @@ -6,6 +6,9 @@ all: $(BIN) %: %.cc $(CXX) -o $* $^ -std=c++17 -g -fsanitize=address -fsanitize=undefined -fsanitize=leak +concepts-20: concepts-20.cc + $(CXX) -fsyntax-only $^ -std=c++20 + fmt: clang-format -i $(SRC) diff --git a/src/development/c++/concepts-20.cc b/src/development/c++/concepts-20.cc new file mode 100644 index 0000000..018b30b --- /dev/null +++ b/src/development/c++/concepts-20.cc @@ -0,0 +1,105 @@ +#include +#include + +// 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 +using Alias = T; + +void print(int); + +// -- CONCEPTS & REQUIRE EXPRESSIONS ------------------------------------------- + +// Simple concept from a type trait. +template +concept Same = std::is_same::value; + +// Simple requirement concepts. +template +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 +concept TraitTypes = requires(T t) { + // T must have a type definition inner. + typename T::inner; + // Type alias must exist. + typename Alias; +}; + +// Compound requirement concepts. +template +concept TraitFns = requires(T t, const T c) { + // void T::foo() must exist. + { t.foo() }; + // bool T::bar() const; must exist. + { c.bar() } -> Same; + // static void T::stat(); must exist. + { T::stat() } -> Same; +}; + +// Nested requirement concepts. +template +concept TraitNested = requires(T t) { + // Must satisfy other concepts. + requires TraitTypes; + requires TraitFns; +}; + +// -- REQUIRE EXPRESSIONS ------------------------------------------------------ + +// Require expressions can be evaluated to booleans. +template +static constexpr bool IsTraitFns = requires { requires TraitFns; }; + +// Require expressions can also be used in static assertions. +static_assert(requires { requires Same; }); +static_assert(!requires { + typename Alias; + requires Same; +}); + +// -- TESTS -------------------------------------------------------------------- + +static_assert(requires { requires TraitAddAndPrint; }); + +struct FnTypeGood { + using inner = int; +}; +struct FnTypeBad {}; +static_assert(requires { requires TraitTypes; }); +static_assert(!requires { requires TraitTypes; }); + +struct FnGood { + void foo(); + bool bar() const; + static int stat(); +}; +struct FnBad {}; +static_assert(requires { requires TraitFns; }); +static_assert(!requires { requires TraitFns; }); + +struct NestedGood : FnTypeGood, FnGood {}; +struct NestedBad1 : FnGood {}; +struct NestedBad2 : FnTypeGood {}; +static_assert(requires { requires TraitNested; }); +static_assert(!requires { requires TraitNested; }); +static_assert(!requires { requires TraitNested; }); -- cgit v1.2.3