Boost logo

Boost :

Subject: [boost] [result_of] decltype and function objects
From: Eric Niebler (eric_at_[hidden])
Date: 2008-11-13 19:43:57


I've noticed test failures when compiling some of boost in C++0x mode
due to the change to boost::result_of to use decltype. The problem comes
from function objects like this:

template<class T>
struct identity
{
   typedef T result_type;

   T const & // <== problem here
   operator()(T const &t) const
   {
       return t;
   }
};

In this (overly simplistic) example, we just want to return what is
passed in. The computed type (result_type) is different than the return
type of operator() ... and for a good (IMO) reason: to avoid an
unnecessary copy in some cases. The idea is to accommodate usage like this:

result_of<identity(BigObj)>::type i = identity()(BigObj());

It is really very important that decltype(i) be BigObj, and not BogObj
const &, which would cause a dangling reference. But with the change to
use decltype in boost::result_of, the dangling reference is exactly what
I would get.

Am I now forced to change identity::operator() return by value here?
That seems sub-optimal to me. In many other potential uses of identity
(e.g., when forwarding the result to some other function), the copy is
totally unnecessary.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk