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-23 03:57:11


Hi Terry,

Terry Golubiewski schrieb:
>
>
> But this generic version doesn't compile...
what you want is a "lazy" version of BOOST_TYPEOF.
By wrapping your expression into BOOST_TYPEOF_NESTED_TYPEDEF you will
delay the calculation until you really need it.

The code below compile and work as expected.

HTH

Kim

# include <boost/mpl/identity.hpp>
# include <boost/typeof/typeof.hpp>

# include <cstddef>

struct MessageHandler1 {
  void f(const char*);
}; // MessageHandler1

struct MessageHandler2 {
  int f(const char*);
}; // MessageHandler2

template<class T,class R>
boost::mpl::identity<R> TypeDeducer(R (T::*)(const char*));

template<class X>
struct GenericDeducer {
  BOOST_TYPEOF_NESTED_TYPEDEF_TPL(Lazy,TypeDeducer(&X::f))
  typedef typename Lazy::type::type type;
}; // GenericDeducer

# include <iostream>
# include <iomanip>
# include <typeinfo>
# include <boost/mpl/assert.hpp>
# include <boost/type_traits/is_same.hpp>

using namespace std;

int main() {
  cout << typeid(GenericDeducer<MessageHandler1>::type).name() << endl;
  cout << typeid(GenericDeducer<MessageHandler2>::type).name() << endl;
} // main


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