template <class T>
class HasPrint1
{
public:
struct type
{
enum { value = ( sizeof(dummy((T*)0)) == sizeof(yes_t) ) };
};
typedef char yes_t;
struct no_t { yes_t[2] m; };
template <class C>
static yes_t dummy(C*, size_t = sizeof(&C::print1));
static no_t dummy(...);
};
// same for HasPrint2
template <class T>
boost::enable_if< HasPrint1<T> > Print(const T& t) { t.print1(); }
template <class T>
boost::enable_if< HasPrint2<T> > Print(const T& t) { t.print2(); }
template <class T>
boost::disable_if< boost::mpl::or_< HasPrint1<T>, HasPrint2<T> > >
Print(const T& t) { std::cout << t << std::endl; }
disable_if
选项以显示与
if / else if / else
我很高兴得到一些评论。