Boost logo

Boost :

Subject: Re: [boost] [core] Breaking change to boost::ref in 1.56
From: Peter Dimov (lists_at_[hidden])
Date: 2014-07-14 06:43:03

Andrey Semashev wrote:

> template< typename T >
> void foo1(T const& t)
> {
> foo(boost::ref(t));
> }

That's getting a bit artificial now. What is the purpose of foo1 except to
break the code?

> The point was that (a) unwrap_reference doesn't remove all nested levels
> of reference_wrappers, just the top level, so class_type does not have bar
> when foo1 is called and (b) boost::bind saves a reference to a reference,
> so get_pointer does not work as intended, not to mention that a dangling
> reference may be left if the function object call is delayed.

If you delay the function call in

> template< typename T >
> void foo(T const& t)
> {
> typedef typename boost::unwrap_reference<T>::type class_type;
> boost::bind(&class_type::bar, boost::ref(t))();
> }

it will result in a dangling reference when called with an rvalue of any
type. This line, for example:

> foo(my_class_const());

will store a dangling reference to the my_class_const temporary.

Boost list run by bdawes at, gregod at, cpdaniel at, john at