From b066e2a66fc44c344a619c42d5319ef59da625bf Mon Sep 17 00:00:00 2001 From: Vladimir Pinchuk Date: Tue, 1 Oct 2024 23:08:31 +0300 Subject: [PATCH] etl/delegate: fix accident creation of a delegate to an rvalue delegate when copying/assigning from delegate with mismatching signature --- include/etl/private/delegate_cpp11.h | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/include/etl/private/delegate_cpp11.h b/include/etl/private/delegate_cpp11.h index 8f438602c..53e31a5b5 100644 --- a/include/etl/private/delegate_cpp11.h +++ b/include/etl/private/delegate_cpp11.h @@ -86,13 +86,24 @@ namespace etl //************************************************************************* /// Declaration. //************************************************************************* - template class delegate; + template + class delegate; + + //************************************************************************* + /// Specialisation - base class of all delegates. + //************************************************************************* + template <> + class delegate + { + protected: + delegate() = default; + }; //************************************************************************* /// Specialisation. //************************************************************************* template - class delegate final + class delegate final : public delegate { public: @@ -111,7 +122,7 @@ namespace etl //************************************************************************* // Construct from lambda or functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 delegate(TLambda& instance) { assign((void*)(&instance), lambda_stub); @@ -120,7 +131,7 @@ namespace etl //************************************************************************* // Construct from const lambda or functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 delegate(const TLambda& instance) { assign((void*)(&instance), const_lambda_stub); @@ -139,7 +150,7 @@ namespace etl //************************************************************************* /// Create from Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_NODISCARD static ETL_CONSTEXPR14 delegate create(TLambda& instance) { @@ -149,7 +160,7 @@ namespace etl //************************************************************************* /// Create from const Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_NODISCARD static ETL_CONSTEXPR14 delegate create(const TLambda& instance) { @@ -257,7 +268,7 @@ namespace etl //************************************************************************* /// Set from Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 void set(TLambda& instance) { assign((void*)(&instance), lambda_stub); @@ -266,7 +277,7 @@ namespace etl //************************************************************************* /// Set from const Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 void set(const TLambda& instance) { assign((void*)(&instance), const_lambda_stub); @@ -427,7 +438,7 @@ namespace etl //************************************************************************* /// Create from Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 delegate& operator =(TLambda& instance) { assign((void*)(&instance), lambda_stub); @@ -437,7 +448,7 @@ namespace etl //************************************************************************* /// Create from const Lambda or Functor. //************************************************************************* - template ::value && !etl::is_same, TLambda>::value, void>> + template ::value && !etl::is_base_of, TLambda>::value, void>> ETL_CONSTEXPR14 delegate& operator =(const TLambda& instance) { assign((void*)(&instance), const_lambda_stub);