I see examples of enable_if used to specify a return type to a function:
template <class T>
typename enable_if_c::type
foo(T t)
{
return t;
}
Is it possible to use enable_if with a constructor? For example, I'd like
to define a constructor that allows only certain values of an enum:
#include
class foo {
public:
enum state {
state1,
state2,
state3
};
// This should compile only when passed (s == state2 or s == state3)
template<int ID>
foo(boost::enable_if_c::type s)
: status_(s) {}
private:
state status_;
};
Even if this turns out to be impossible, I'd like to go on and try to
define some_boolean_condition if I may. I see that Aleksey plans a
contains_c metafunction for 1.33,
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?MPL_TODO_List
Equivalent URL: http://tinyurl.com/4toly
so my limited understanding leads me to
some_boolean_condition is equivalent to:
mpl::contains_c< mpl::vector_c< int, state2, state3 >, ID >::value
Does that look correct?
In the absence of contains_c, I've been trying to use mpl::find_if but my
predicate compiles for both "valid" and "invalid" values, so I must be
doing something wrong.
I'd be grateful if someone could put me back on track.
Regards,
Angus
#include
#include
#include
#include
#include
#include
#include
#include
#include
class foo {
public:
enum state {
state1,
state2,
state3
};
};
int main()
{
// Should lead to a compile-time failure.
#define TESTED_VALUE foo::state1
// Should compile.
// #define TESTED_VALUE foo::state2
namespace mpl = boost::mpl;
typedef mpl::vector_c< int, foo::state2, foo::state3 >
allowable_values;
typedef
mpl::find_if<
allowable_values,
mpl::equal_to<
mpl::_1,
mpl::integral_c
>
>::type iter;
BOOST_MPL_ASSERT((
mpl::not_<
boost::is_same<
iter::type,
mpl::end::type
>
>
));
return 0;
}