Boost logo

Boost Users :

Subject: Re: [Boost-users] How to deduce (possibly void) return type of member function?
From: Kim Kuen Tang (kuentang_at_[hidden])
Date: 2010-05-22 13:18:07


Terry Golubiewski schrieb:
> How? I read the docs you referenced (previously), but I did not see a
> concrete example.

i thought it was straightforward, but looking into details it was not
the right reference. =-O

However, below is a solution which relies on BOOST_TYPEOF. BOOST_TYPEOF
will have a problem if the return_type is void.
So i replace it with mpl::void_.
> typedef typename boost::result_of< ...what goes here???...
> (double)>::type f_double_return_type;
>
> terry

HTH

Kim

# include <boost/mpl/assert.hpp>
# include <boost/typeof/typeof.hpp>
# include <boost/type_traits/is_same.hpp>
# include <boost/mpl/if.hpp>
# include <boost/mpl/void.hpp>

struct X
{
    int f(int)
    {
        return 1;
    }
   
    double f(double)
    {
        return 1.0;
    }
   
    void f(char)
    {}
};

template<typename Param, typename return_type>
typename boost::mpl::if_<
    boost::is_same<return_type,void>
  , boost::mpl::void_
  , return_type
>::type
deduce_type(return_type (X::*)(Param));

typedef BOOST_TYPEOF(deduce_type<char>(&X::f)) void_;
typedef BOOST_TYPEOF(deduce_type<int>(&X::f)) int_;
typedef BOOST_TYPEOF(deduce_type<double>(&X::f)) double_;

int main() {

  BOOST_MPL_ASSERT((boost::is_same<void_,boost::mpl::void_>));
  BOOST_MPL_ASSERT((boost::is_same<int_,int>));
  BOOST_MPL_ASSERT((boost::is_same<double_,double>));
  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net