From a6d3ae16736d7746ad6827f10c299ad84aa78a5b Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 12 Apr 2023 22:41:33 +0200 Subject: c++: add example for picking template with partial specialzations --- src/development/c++.md | 4 +++ src/development/c++/Makefile | 5 ++- src/development/c++/concepts-11.cc | 3 +- src/development/c++/tmpl-pair.cc | 73 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/development/c++/tmpl-pair.cc (limited to 'src') diff --git a/src/development/c++.md b/src/development/c++.md index e6fcfef..9dc8fb0 100644 --- a/src/development/c++.md +++ b/src/development/c++.md @@ -69,6 +69,10 @@ struct is_valid `std::declval()` creates an instance of type T in an unevaluated context. +## Template selection with partially / fully specializations. +```cpp +{{#include c++/tmpl-pair.cc:3:}} +``` [gist-strict-asliasing]: https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8 [parameter-pack]: https://en.cppreference.com/w/cpp/language/parameter_pack diff --git a/src/development/c++/Makefile b/src/development/c++/Makefile index 610b5a2..1d5172b 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 +SRC = concepts-11.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc BIN = $(SRC:.cc=) all: $(BIN) @@ -6,5 +6,8 @@ all: $(BIN) %: %.cc $(CXX) -o $* $^ -std=c++17 -g -fsanitize=address -fsanitize=undefined -fsanitize=leak +fmt: + clang-format -i $(SRC) + clean: $(RM) $(BIN) diff --git a/src/development/c++/concepts-11.cc b/src/development/c++/concepts-11.cc index 888ff4d..cec8716 100644 --- a/src/development/c++/concepts-11.cc +++ b/src/development/c++/concepts-11.cc @@ -32,8 +32,7 @@ struct is_entry { template using val = decltype(std::declval().val()); - static constexpr bool value = is_valid_v && - is_valid_with_ret_v && + static constexpr bool value = is_valid_v && is_valid_with_ret_v && is_valid_with_ret_v; }; diff --git a/src/development/c++/tmpl-pair.cc b/src/development/c++/tmpl-pair.cc new file mode 100644 index 0000000..a8acaed --- /dev/null +++ b/src/development/c++/tmpl-pair.cc @@ -0,0 +1,73 @@ +// Copyright (C) 2023 johannst + +enum Kind { + kPrimary, + kTT, + kIntBool, + kIntInt, +}; + +// (1) Primary template. +template +struct pair { + static constexpr Kind kind = kPrimary; +}; + +// (2) Partial template specialization. +template +struct pair { + static constexpr Kind kind = kTT; +}; + +// (3) Template specialization. +template<> +struct pair { + static constexpr Kind kind = kIntBool; +}; + +// (4) Template specialization. +template<> +struct pair { + static constexpr Kind kind = kIntInt; +}; + +int main() { + static_assert(pair::kind == kIntBool, ""); + // * Compare template arg list with primary template, we only supplied one + // arg, the second one will be defaulted as + // pair + // * Compare template arg list against available specializations, this will + // try to match the pattern against the patterns defined in the + // partial specializations. + // * (2) pattern does not match + // * (3) pattern does match + // * (4) pattern does not match + // * Pick the most specialized version -> (3) + + static_assert(pair::kind == kTT, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern against the patterns defined in the + // partial specializations. + // * (2) pattern does match + // * (3) pattern does not match + // * (4) pattern does not match + // * Pick the most specialized version -> (2) + + static_assert(pair::kind == kIntInt, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern against the patterns defined in the + // partial specializations. + // * (2) pattern does match + // * (3) pattern does match + // * (4) pattern does not match + // * Pick the most specialized version -> (3) + + static_assert(pair::kind == kPrimary, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern against the patterns defined in the + // partial specializations. + // * (2) pattern does not match + // * (3) pattern does not match + // * (4) pattern does not match + // * No specialization matches, take the primary template. +} -- cgit v1.2.3