On Monday 14 July 2014 13:43:03 Peter Dimov wrote:
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?
Well, you might want to do something on t before invoking the common logic in foo. I admit I invented this example just now, but I can imagine something like this in real life.
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.
Right. That comment about the dangling reference was more related to the call of foo1.