diff options
author | johannst <johannst@users.noreply.github.com> | 2023-04-12 20:57:45 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2023-04-12 20:57:45 +0000 |
commit | 4b3a334cf553b9f0b7f831538cb4d23fbdb0594f (patch) | |
tree | 3a8e4df94497b437656454bc29e522b56f9765cb /development/c++/tmpl-pair.cc | |
parent | 3f1d25c3e62f91f2555f0649c7c71f3b730717be (diff) | |
download | notes-4b3a334cf553b9f0b7f831538cb4d23fbdb0594f.tar.gz notes-4b3a334cf553b9f0b7f831538cb4d23fbdb0594f.zip |
deploy: d6deac54dd372c946942637636846893694ce521
Diffstat (limited to 'development/c++/tmpl-pair.cc')
-rw-r--r-- | development/c++/tmpl-pair.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/development/c++/tmpl-pair.cc b/development/c++/tmpl-pair.cc new file mode 100644 index 0000000..a8acaed --- /dev/null +++ b/development/c++/tmpl-pair.cc @@ -0,0 +1,73 @@ +// Copyright (C) 2023 johannst + +enum Kind { + kPrimary, + kTT, + kIntBool, + kIntInt, +}; + +// (1) Primary template. +template<typename T, typename U = bool> +struct pair { + static constexpr Kind kind = kPrimary; +}; + +// (2) Partial template specialization. +template<typename T> +struct pair<T, T> { + static constexpr Kind kind = kTT; +}; + +// (3) Template specialization. +template<> +struct pair<int, bool> { + static constexpr Kind kind = kIntBool; +}; + +// (4) Template specialization. +template<> +struct pair<int, int> { + static constexpr Kind kind = kIntInt; +}; + +int main() { + static_assert(pair<int>::kind == kIntBool, ""); + // * Compare template arg list with primary template, we only supplied one + // arg, the second one will be defaulted as + // pair<int, bool> + // * Compare template arg list against available specializations, this will + // try to match the pattern <int, bool> against the patterns defined in the + // partial specializations. + // * (2) <int, bool> pattern does not match + // * (3) <int, bool> pattern does match + // * (4) <int, bool> pattern does not match + // * Pick the most specialized version -> (3) + + static_assert(pair<char, char>::kind == kTT, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern <char, char> against the patterns defined in the + // partial specializations. + // * (2) <char, char> pattern does match + // * (3) <char, char> pattern does not match + // * (4) <char, char> pattern does not match + // * Pick the most specialized version -> (2) + + static_assert(pair<int, int>::kind == kIntInt, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern <int, int> against the patterns defined in the + // partial specializations. + // * (2) <int, int> pattern does match + // * (3) <int, int> pattern does match + // * (4) <int, int> pattern does not match + // * Pick the most specialized version -> (3) + + static_assert(pair<char, short>::kind == kPrimary, ""); + // * Compare template arg list against available specializations, this will + // try to match the pattern <char, short> against the patterns defined in the + // partial specializations. + // * (2) <char, short> pattern does not match + // * (3) <char, short> pattern does not match + // * (4) <char, short> pattern does not match + // * No specialization matches, take the primary template. +} |