Boost logo

Boost :

Subject: Re: [boost] result_of overloaded member function?
From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2011-05-05 06:13:09


2011/5/4 Arno Schödl <aschoedl_at_[hidden]>:
> Hello,
>
>
>
> is there a way to get the result type of an overloaded member function? Something like:
>
>
>
> template< class T, class A >
>
> typename boost::result_of< T::func(A) >::type invoke( T& t, A& a ) {
>
> return t.func(a);
>
> }

Another solution based on decltype. Maybe interesting for you, if you
already use c++0x. This code compiles using msvc-10.

Regards,
Joachim

//========================================================
class T0
{
public:
  int func(const int& x)const
  { cout << "T0::func(int) " << x << endl; return x; };
  float func(const float& x)const
  { cout << "T0::func(float) " << x << endl; return x; };
};

class T1
{
public:
  template<class A>
  A func(const A& x)const
  { cout << "T1::func(A) " << x << endl; return x; };
};

template< class T, class A >
typename decltype( T().func(A()) ) invoke( T& t, A& a )
{
  return t.func(a);
};

BOOST_AUTO_TEST_CASE(overladed_member_function_call_2)
{
  T0 t0;
  int i = 42;
  float f = 43;

  invoke(t0, i);
  invoke(t0, f);

  T1 t1;
  invoke(t1, i);
  invoke(t1, f);
}
//========================================================

-- 
Interval Container Library [Boost.Icl]
http://www.joachim-faulhaber.de

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