From f4211efc27489e20ee7dff39cfeca81ffa922c3e Mon Sep 17 00:00:00 2001 From: Tobias Zindl Date: Thu, 5 Oct 2023 22:26:48 +0200 Subject: [PATCH 1/2] :adhesive_bandage: change typeof for utest gcc build --- tests/libs/utest/utest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libs/utest/utest.h b/tests/libs/utest/utest.h index a6fd773..93f3547 100644 --- a/tests/libs/utest/utest.h +++ b/tests/libs/utest/utest.h @@ -672,7 +672,7 @@ utest_type_printer(long long unsigned int i) { #endif #else -#define UTEST_AUTO(x) typeof(x + 0) +#define UTEST_AUTO(x) __typeof__(x + 0) #endif #if defined(__clang__) From 97b78a945fe247c38ea0ff513bc1ca7408cc572e Mon Sep 17 00:00:00 2001 From: Tobias Zindl Date: Thu, 5 Oct 2023 22:27:16 +0200 Subject: [PATCH 2/2] :sparkles: add integral constant --- include/zll/meta/integral_constant.hpp | 27 +++++++++++++++ tests/unit_tests/meson.build | 2 +- .../meta/integral_constant_tests.cpp | 33 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 include/zll/meta/integral_constant.hpp create mode 100644 tests/unit_tests/meta/integral_constant_tests.cpp diff --git a/include/zll/meta/integral_constant.hpp b/include/zll/meta/integral_constant.hpp new file mode 100644 index 0000000..3cb3f32 --- /dev/null +++ b/include/zll/meta/integral_constant.hpp @@ -0,0 +1,27 @@ +#ifndef ZLL_META_INTEGRALCONSTANT_HPP +#define ZLL_META_INTEGRALCONSTANT_HPP + +namespace zll { +namespace meta { + +template +struct integral_constant { + typedef T value_type; + typedef integral_constant type; + + static const value_type value = VALUE_V; + + operator value_type() const { return VALUE_V; } + value_type operator()() const { return VALUE_V; } +}; + +template +struct bool_constant : integral_constant {}; + +typedef bool_constant true_type; +typedef bool_constant false_type; + +} // namespace meta +} // namespace zll + +#endif // ZLL_META_INTEGRALCONSTANT_HPP diff --git a/tests/unit_tests/meson.build b/tests/unit_tests/meson.build index cec2190..cf821f8 100644 --- a/tests/unit_tests/meson.build +++ b/tests/unit_tests/meson.build @@ -1,5 +1,6 @@ unit_tests_srcs = [ + 'meta/integral_constant_tests.cpp', 'main.cpp', ] @@ -9,7 +10,6 @@ unit_tests_exec = executable( unit_tests_srcs, include_directories: unit_tests_inc_dir, dependencies: [utest_dep, dis_dep], - override_options: ['cpp_std=c++20', 'warning_level=3'], ) test('zll-unit-tests', unit_tests_exec, suite: 'dis') diff --git a/tests/unit_tests/meta/integral_constant_tests.cpp b/tests/unit_tests/meta/integral_constant_tests.cpp new file mode 100644 index 0000000..abd0449 --- /dev/null +++ b/tests/unit_tests/meta/integral_constant_tests.cpp @@ -0,0 +1,33 @@ +#include "zll/meta/integral_constant.hpp" +#include "zll/debug/static_assert.hpp" + +#include + +UTEST(meta_integral_constant, int_constant) { + typedef zll::meta::integral_constant two_t; + + const two_t two = two_t(); + ASSERT_EQ(two_t::value, 2); + ASSERT_EQ(two(), 2); + const int val = two; + + ASSERT_EQ(val, 2); + ZLL_STATIC_ASSERT((two_t::value == 2)); +} + +UTEST(meta_integral_constant, bool_constant) { + { + const zll::meta::true_type true_v; + ASSERT_TRUE((zll::meta::true_type::value)); + ASSERT_TRUE(true_v); + ASSERT_TRUE(true_v()); + ZLL_STATIC_ASSERT((zll::meta::true_type::value)); + } + { + const zll::meta::false_type false_v; + ASSERT_FALSE((zll::meta::false_type::value)); + ASSERT_FALSE(false_v); + ASSERT_FALSE(false_v()); + ZLL_STATIC_ASSERT(!(zll::meta::false_type::value)); + } +}