From d6deac54dd372c946942637636846893694ce521 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 12 Apr 2023 22:57:27 +0200 Subject: c++: improve void_t explanation --- src/development/c++/Makefile | 2 +- src/development/c++/tmpl-void_t.cc | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/development/c++/tmpl-void_t.cc (limited to 'src/development/c++') diff --git a/src/development/c++/Makefile b/src/development/c++/Makefile index 1d5172b..ca752d9 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 +SRC = concepts-11.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc tmpl-void_t.cc BIN = $(SRC:.cc=) all: $(BIN) diff --git a/src/development/c++/tmpl-void_t.cc b/src/development/c++/tmpl-void_t.cc new file mode 100644 index 0000000..fd95315 --- /dev/null +++ b/src/development/c++/tmpl-void_t.cc @@ -0,0 +1,47 @@ +// Copyright (C) 2023 johannst + +#include + +// (1) Primary template. +template +struct is_valid : std::false_type {}; + +// (2) Partial template specialization. +template +struct is_valid().some_fun1()), + decltype(std::declval().some_fun2())>> : std::true_type {}; +struct A { + void some_fun1() {} + void some_fun2() {} +}; + +struct B {}; + +static_assert(is_valid::value, "is true"); +// * Compare template arg list with primary template, we only supplied one +// arg, the second one will be defaulted as +// is_valid +// * Compare template arg list against available specializations, this will +// try to match the pattern against the patterns defined in the +// partial specializations. +// * Try specialization (2) +// * T -> A +// * Evaluate std::void_t -> decltype's are well-formed +// std::void_t<...> -> void +// * Specialization (2) matches +// * Pick the most specialized version -> (2) + +static_assert(!is_valid::value, "is false"); +// * Compare template arg list with primary template, we only supplied one +// arg, the second one will be defaulted as +// is_valid +// * Compare template arg list against available specializations, this will +// try to match the pattern against the patterns defined in the +// partial specializations. +// * Try specialization (2) +// * T -> B +// * Evaluate std::void_t -> decltype's are ill-formed +// * Specialization (2) is removed from candidate set, no hard error (SFINAE) +// * No specialization matches, take the primary template. + +int main() {} -- cgit v1.2.3