result_of overloaded member function?

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); } Thanks for help, Arno -- Dr. Arno Schödl | aschoedl@think-cell.com Technical Director think-cell Software GmbH | Chausseestr. 8/E | 10115 Berlin | Germany http://www.think-cell.com | phone +49 30 666473-10 | US phone +1 800 891 8091 Amtsgericht Berlin-Charlottenburg, HRB 85229 | European Union VAT Id DE813474306 Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl

2011/5/4 Arno Schödl <aschoedl@think-cell.com>:
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);
}
Would this be helpful? class C { public: int f(int); float f(int, float); }; BOOST_AUTO_TEST_CASE(decl_type) { BOOST_CHECK((boost::is_same< int, decltype( C().f(0) )>::value)); BOOST_CHECK((boost::is_same<float, decltype( C().f(0, 0.0) )>::value)); } Regards, Joachim -- Interval Container Library [Boost.Icl] http://www.joachim-faulhaber.de

On Wed, May 4, 2011 at 10:02 AM, Arno Schödl <aschoedl@think-cell.com> wrote:
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);
}
Not exactly. The template parameters to result_of are types. In your example, assuming func is the name of an overloaded member function, T::func is not a type. To call a pointer to member function, assuming func is a template, you could do the following. template< class T, class F, class A > typename boost::result_of< F(A) >::type invoke_ptr(T t, F f, A a) { return (t.*f)(a); } struct S0 { template<class T> T func(T x) { return x; } }; invoke_ptr(S0(), &S0::func<int>, 1); invoke_ptr(S0(), &S0::func<float>, 0.1); It would also work with non-template overloads, but then you need to specify the type of the member function pointer explicitly. struct S1 { int func(int x) { return x; } float func(float x) { return x; } }; invoke_ptr<S1, int(S1::*)(int), int>(S1(), &S1::func, 1); invoke_ptr<S1, float(S1::*)(float), float>(S1(), &S1::func, 0.1); Now, let's assume instead that func is actually a callable object type; e.g. a functor. Then you could do something like the following, which is closer to your original example. template< class T, class A > typename boost::result_of< typename T::func(A) >::type invoke(T t, A a) { return typename T::func()(a); } struct S2 { struct func { template<class T> T operator()(T x) { return x; } }; }; invoke(S2(), 1); invoke(S2(), 0.1); I compiled these examples on boost trunk with g++ 4.5 with c++0x and BOOST_RESULT_OF_USE_DECLTYPE defined. But you could use the TR1 result_of protocol to make the last example work in C++03 as well. Hope that helps! Daniel Walker

2011/5/4 Arno Schödl <aschoedl@think-cell.com>:
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
participants (3)
-
Arno Schödl
-
Daniel Walker
-
Joachim Faulhaber