From fac9e8bec1983fa9dff8f447fef106e427dfec26 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 20 Jul 2023 17:02:05 +0200 Subject: c++: Renamed src to c++ --- c++/core/templates.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 c++/core/templates.h (limited to 'c++/core/templates.h') diff --git a/c++/core/templates.h b/c++/core/templates.h new file mode 100644 index 0000000..39befc1 --- /dev/null +++ b/c++/core/templates.h @@ -0,0 +1,76 @@ +#pragma once + +#include "string_literal.h" + +namespace saw { + +template struct parameter_pack_index; + +template struct parameter_pack_index { + static constexpr size_t value = 0u; +}; + +template +struct parameter_pack_index { + static constexpr size_t value = + 1u + parameter_pack_index::value; +}; + +template struct parameter_pack_type { + static_assert(always_false, "Should've been caught by the specializations"); +}; + +template struct parameter_pack_type<0, TN, T...> { + using type = TN; +}; + +template +struct parameter_pack_type { + static_assert(sizeof...(T) > 0, "Exhausted parameters"); + static_assert(N > 0, "Invalid number. Should've been caught"); + using type = typename parameter_pack_type::type; +}; +/* + * Nightmare inducing compiler problems found here. Somehow non-type + * string_literals cannot be resolved as non-type primitive template values can. + * This is the workaround + */ +template +struct parameter_key_pack_index_helper { + static constexpr size_t value = + (V == Key0) + ? (0u) + : (1u + parameter_key_pack_index_helper::value); +}; + +template +struct parameter_key_pack_index_helper { + static constexpr size_t value = (V == Key0) ? (0u) : (1u); +}; + +template +struct parameter_key_pack_index { + static constexpr size_t value = + parameter_key_pack_index_helper::value; + static_assert(value < sizeof...(Keys), + "Provided string_literal doesn't exist in searched list"); +}; + +template +struct parameter_key_pack_type_helper { + static constexpr string_literal literal = parameter_key_pack_type_helper::literal; +}; + +template +struct parameter_key_pack_type_helper { + static constexpr string_literal literal = Key0; +}; + +template +struct parameter_key_pack_type { + static constexpr string_literal literal = parameter_key_pack_type_helper::literal; + + static_assert(i < sizeof...(Keys), "Provided index is too large in list"); +}; + +} -- cgit v1.2.3