From 8e02bb305929325a8187624eab5996a01656fca5 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 12 Jul 2025 12:53:31 +0100 Subject: [PATCH] Strengthened static assert tests Changed enable_if checks to static asserts --- include/etl/closure.h | 25 ++++++++++++++----------- test/test_closure.cpp | 10 ++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/etl/closure.h b/include/etl/closure.h index cb710944..cb9e372c 100644 --- a/include/etl/closure.h +++ b/include/etl/closure.h @@ -35,6 +35,7 @@ SOFTWARE. #include "delegate.h" #include "tuple.h" #include "utility.h" +#include "type_list.h" namespace etl { @@ -62,9 +63,9 @@ namespace etl { public: - using delegate_type = etl::delegate; ///< The delegate type to be invoked. - using tuple_type = etl::tuple; ///< Tuple type for storing arguments. - + using delegate_type = etl::delegate; ///< The delegate type to be invoked. + using argument_types = etl::type_list; ///< The type list of arguments. + //********************************************************************* /// Construct a closure with a delegate and its arguments. /// \param f The delegate to be invoked. @@ -92,16 +93,18 @@ namespace etl /// \tparam TArg Type of the argument. /// \param arg The new value to bind. //********************************************************************* - template >::value && !etl::is_reference::value>> - void bind(TArg arg) + template + void bind(UArg arg) { - etl::get(m_args) = arg; + static_assert(etl::is_convertible>::value, "Argument is not convertible"); + static_assert(!etl::is_reference::value, "Cannot bind reference arguments"); + + etl::get(m_args) = arg; } //********************************************************************* /// Bind new values to multiple arguments at once. - /// The number and types of arguments must match the tuple. + /// The number of arguments must match the tuple. /// \param args The new values to bind. ///********************************************************************* template @@ -120,8 +123,8 @@ namespace etl template void bind_impl(etl::index_sequence, UArgs&&... args) { - // Use an initializer list to expand the pack and assign each argument - int dummy[] = {0, (etl::get(m_args) = etl::forward(args), 0)...}; + // Expand the pack and call bind(arg) for each argument + int dummy[] = {0, (bind(etl::forward(args)), 0)...}; (void)dummy; // Suppress unused variable warning } @@ -137,7 +140,7 @@ namespace etl } delegate_type m_f; ///< The delegate to invoke. - tuple_type m_args; ///< The bound arguments. + etl::tuple m_args; ///< The bound arguments. }; #else //************************************************************************* diff --git a/test/test_closure.cpp b/test/test_closure.cpp index 10d2572d..6d200925 100644 --- a/test/test_closure.cpp +++ b/test/test_closure.cpp @@ -151,5 +151,15 @@ namespace CHECK_EQUAL(23, c5()); } + //************************************************************************* + TEST(test_bind_static_assert) + { + etl::closure c(df2, 1, 2); + + // Uncomment to generate static_assert errors. + //c.bind(1); // Argument count mismatch + //c.bind(1, 2, 3); // Argument count mismatch + //c.bind(1, std::string()); // Argument is not convertible + } }; }