workaround user taking ref to temporary

If D is a temporary, it's lifetime extension is done
when we go out of the scope of choose_parameter
so the const D& returned is dangling
This commit is contained in:
Sébastien Loriot 2021-09-03 11:03:22 +02:00
parent abddb30bd7
commit 1eda841d32
2 changed files with 10 additions and 1 deletions

View File

@ -313,6 +313,12 @@ const D& choose_parameter(const internal_np::Param_not_found&, const D& d)
return d;
}
template <typename D>
D choose_parameter(const internal_np::Param_not_found&, D&& d)
{
return d;
}
template <typename T, typename D>
T& choose_parameter(T& t, D&)
{

View File

@ -43,10 +43,13 @@ void test_values_and_types(const NamedParameters& np)
}
template<class NamedParameters>
void test_no_copyable(const NamedParameters&)
void test_no_copyable(const NamedParameters& np)
{
typedef typename inp::Get_param<typename NamedParameters::base,inp::visitor_t>::type NP_type;
static_assert( boost::is_same<NP_type,std::reference_wrapper<const B> > ::value );
const A<4>& a = params::choose_parameter(params::get_parameter_reference(np, inp::edge_index), A<4>(4));
assert(a.v==4);
}
template <class NamedParameters>