Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-04-05 09:11:37


Angus Leeming <angus.leeming_at_[hidden]> writes:

> Is it possible to use enable_if with a constructor?

In principle, yes. You just add a dummy default parameter for the
enabler.

> For example, I'd like to define a constructor that allows only
> certain values of an enum:

No chance; that's a runtime test... or you'd have to specify the enum
as a template argument to the ctor explicitly... but you can't provide
explicit template arguments to ctors.

Well, you can do something like:

  template <class N>
  foo(
        N

      , typename boost::enable_if<
            typename contains_c<
                mpl::vector_c<state, state2, state3>
              , N
>::type
>::type*=0
  );
  
where

  template <class S, class N>
  struct contains_c
    : mpl::not_<
          mpl::is_same<
              typename mpl::find_if<S,mpl::equal_to<N,_> >::type
            , typename mpl::end<S>::type
>
>
  {};

and then a foo is constructed something like this:

    foo x( mpl::int_<state2>() );

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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