Boost logo

Boost :

Subject: Re: [boost] enable_if and non-template member functions?
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2011-08-12 14:14:17


On Fri, 12 Aug 2011 02:31:10 -0700, John Maddock <boost.regex_at_[hidden]>
wrote:

>> From your description, I think I've done exactly this. E.g.
>
> --8<---------------cut here---------------start------------->8---
> template <class U>
> struct X
> {
> // member function void f(int) becomes something like:
> template <class T>
> typename enable_if<
> mpl::and_<
> is_convertible<T,int>
> , some_condition_on<U>
> >
> >::type
> f(T x_)
> { int x = x_; ... }
> };
> --8<---------------cut here---------------end--------------->8---
>
> That works as long as the function has at least one parameter that can
> be turned into a template, but I have some cases that are operators or
> else have no parameters.

In case of no-parameter functions or operators, what's wrong with:

template <class U>
struct T
{
     void f()
     {
         f_impl(<static_cast<void *>(0));
     }

private:
     template <class T>
     typename enable_if<
         mpl::and_<
         is_convertible<T, void *>,
         some_condition_on<U>
>::type
      f_impl(T)
      { .... }
};

> BTW, it would be great if all these techniques could be added to the
> enable_if docs.

+1

It would be great if all the docs had a best-practices section and a
commonly-encountered-techniques section.

-Mostafa


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