From f52865b5011eedd3ebe310d37852a4394ae28686 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 25 Nov 2019 06:56:06 +0100 Subject: [PATCH] disallow temporaries to bind to non-const references --- .../src/abstraction/ValueHolderInterface.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/alib2abstraction/src/abstraction/ValueHolderInterface.hpp b/alib2abstraction/src/abstraction/ValueHolderInterface.hpp index 1995d0130f..aa6849fa93 100644 --- a/alib2abstraction/src/abstraction/ValueHolderInterface.hpp +++ b/alib2abstraction/src/abstraction/ValueHolderInterface.hpp @@ -29,6 +29,10 @@ public: return m_isConst; } + bool isRef ( ) const { + return isRvalueRef ( ) || isLvalueRef ( ); + } + bool isRvalueRef ( ) const { return m_isRvalueRef; } @@ -73,7 +77,12 @@ ParamType retrieveValue ( const std::shared_ptr < abstraction::Value > & param, return std::move ( interface->getValue ( ) ); else throw std::domain_error ( "Cannot bind without move" ); + } else if constexpr ( std::is_lvalue_reference_v < ParamType > && std::is_const_v < std::remove_reference_t < ParamType > > ) { + Type && res = interface->getValue ( ); + return res; } else if constexpr ( std::is_lvalue_reference_v < ParamType > ) { + if ( interface->isAutoMove ( ) && ! interface->isRef ( ) ) + throw std::domain_error ( "Cannot bind temporary to non-const reference" ); Type && res = interface->getValue ( ); return res; } else if constexpr ( std::is_copy_constructible_v < Type > && std::is_move_constructible_v < Type > ) { -- GitLab