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 11:15:17


Hi Terry,

Terry Golubiewski schrieb:
> If I have a class...
>
> struct X {
> int f(double x);
> void f(int y);
> };
>
> .... and a template Y that wants to deduce the return-type of some
> member functions of T...
>
> template<class T>
> struct Y {
> typedef typename what_goes_here<T>::type f_double_return_type;
> typedef typename what_goes here<T>::type f_int_return_type;
> };

you should use boost::result_of to do such of things.

See
http://www.boost.org/doc/libs/1_43_0/libs/utility/utility.htm#result_of .
Here a description.

The class template |result_of| helps determine the type of a call
expression. Given an lvalue |f| of type |F| and lvalues |t1|, |t2|, ...,
|t/N/| of types |T1|, |T2|, ..., |T/N/|, respectively, the type
|result_of<F(T1, T2, ..., T/N/)>::type| defines the result type of the
expression |f(t1, t2, ...,t/N/)|. The implementation permits the type
|F| to be a function pointer, function reference, member function
pointer, or class type. When |F| is a class type with a member type
|result_type|, |result_of<F(T1, T2, ..., T/N/)>| is |F::result_type|.
Otherwise, |result_of<F(T1, T2, ..., T/N/)>| is |F::result<F(T1, T2,
...., T/N/)>::type| when |/N/ > 0| or |void| when |/N/ = 0|. For
additional information about |result_of|, see the C++ Library Technical
Report, N1836
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf>, or,
for motivation and design rationale, the |result_of| proposal
<http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1454.html>.

Kim

>
> I"ve tried using...
>
> typedef BOOST_TYPEOF_TPL(static_cast<X*>(0)->f(double(0)))
> f_double_type; // this works
>
> .... but ...
>
> typedef BOOST_TYPEOF_TPL(static_cast<X*>(0)->f(int(0)) f_int_type; //
> does not work for "void"
>
> Of course, I wouldn't know which one returns "void" or not ahead of time.
>
> terry
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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