Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-02-24 09:57:24


The following patch to boost/lambda/detail/return_type_traits.hpp enables it
to extract the return type information from a nested ::result_type typedef
instead of insisting on a sig<> template. This is useful since all standard
function objects in <functional> and many user-defined function objects
(including those produced by boost::mem_fn) have result_type but no sig<>.

I have not commited the patch yet, even though it seems to work fine for me,
since it alters the Lambda behavior in the case where a function object
defines both result_type and sig<>. Currently, sig<> is used. With the
patch, result_type would override sig<>.

All function objects known to man that have both do define sig<> to return
the same type as result_type; nevertheless, I'd like confirmation from the
maintainers first, before I commit.

Thanks for listening; patch follows:

*** 17,22 ****
--- 17,24 ----
  #ifndef BOOST_LAMBDA_RETURN_TYPE_TRAITS_HPP
  #define BOOST_LAMBDA_RETURN_TYPE_TRAITS_HPP

+ #include "boost/mpl/aux_/has_xxx.hpp"
+
  #include <cstddef> // needed for the ptrdiff_t

  namespace boost {
***************
*** 239,244 ****
--- 241,265 ----
    typedef Ret type;
  };

+ // ::result_type support
+
+ namespace detail
+ {
+
+ BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
+
+ template<class F> struct get_result_type
+ {
+ typedef typename F::result_type type;
+ };
+
+ template<class F, class A> struct get_sig
+ {
+ typedef typename function_adaptor<F>::template sig<A>::type type;
+ };
+
+ } // namespace detail
+
    // Ret is detail::unspecified, so try to deduce return type
  template<int I, class Args>
  struct return_type_N<function_action<I, detail::unspecified>, Args > {
***************
*** 251,257 ****
  public:
    // pass the function to function_adaptor, and get the return type from
    // that
! typedef typename function_adaptor<plain_Func>::template sig<Args>::type
type;
  };

--- 272,282 ----
  public:
    // pass the function to function_adaptor, and get the return type from
    // that
! typedef typename detail::IF<
! detail::has_result_type<plain_Func>::value,
! detail::get_result_type<plain_Func>,
! detail::get_sig<plain_Func, Args>
! >::RET::type type;
  };

--
Peter Dimov
http://www.pdimov.com

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