Re: Help with boost::enable_if
german diago <germandiago@gmail.com> wrote:
Anyone knows why this code does not work?
ClassName(typename boost::disable_if<std::is_const<T>>::type * =
0) ...
ClassName(typename boost::enable_if<std::is_const<T>>::type * =
0) ...
The error is about boost::enable_if. I want to enable a constructor
based on the type (if the
type is const, enable the one below else enable the one above. The
error is:
hola.cpp:87: error: no type named ?type' in ?struct
boost::enable_if<std::is_const<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, void>'
That is the (simplified version of the) error. If I remove the one
below, the code works, because I instantiated the class as
ClassName<std::string> but if I add the template above, the compiler
complains.
Thanks for your time
boost::enable_if/disable_if are fairly simple templates that
take a wrappted boolean type [I think its boost::mpl::bool<...>
that is either boost::mpl::true_ or boost::mpl::false, one case
is an ampty struct and the other is an struct containing a nested
typedef of the second template parameter whic defaults to void
that is:
template <class B,class T = void> struct enable_if{};
template <class T> struct enable_if<boost::mpl::true_,T<
{
tyoedef T type;
};
similar for di
so you want something like:
ClassName
(
typename boost::enable_if
<
boost::is_const<T>,
T *
>::type * p = 0
) // rest of ctor for const T *.
simiiliar with disable_if for non const T *.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]