Subject: Re: [boost] [test] BOOST_TEST macro and std::max
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2016-01-11 08:15:18
2016-01-11 13:46 GMT+01:00 Raffi Enficiaud <raffi.enficiaud_at_[hidden]>
> Le 11/01/16 10:51, Andrzej Krzemienski a Ã©crit :
> 2016-01-11 4:11 GMT+01:00 Gennadiy Rozental <rogeeff_at_[hidden]>:
>> Benedek Thaler <thalerbenedek <at> gmail.com> writes:
>>> I believe at this point we understand WHAT is going on.
>>> auto const& E = ::boost::test_tools::assertion::seed()->*3u
>>>> == ((std::max)(0u, 3u));
>>> max returns unsigned int&&, which for some unclear reason is bound to
>>> unsigned int const& in expression template E. This obviously becomes
>>> dangling reference as soon as we leave this line.
>> This is an expected behavior in C++: an xvalue (rvalue reference returned
>> from a function) can be bound to a const lvalue reference. See example:
> This is a nice web site :) (I should update myself)
> The example here is more what is happening:
> the "printit" function with an universal ref. The return of std::max with
> two identical types is a const lvalue ref according to the standard.
> That ref lifetime is expanded until what point?
> The "binary_expr" that is returned with boost.test holds that ref. The
> code is
> template<typename T>
> binary_expr<ExprType, T, op::EQ<unsigned int, T> >
> operator==( T&& rhs )
> // ...
> Is this ref valid at the return of operator== ?
> In the case of
> auto const& E = ::boost::test_tools::assertion::seed()->*3u ==
> ((std::max)(0u, 3u)) ;
> that ref would exists until " ; ", but if "E" contains an object having
> this reference, it is dandling after the " ; ".
Yes, a member reference inside E is dangling after " ; ". Member references
do not prolong the life-time of temporary objects.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk