Boost logo

Boost Users :

Subject: Re: [Boost-users] how to detect if a type is among a know list inside enable_if
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-03-11 21:16:04


AMDG

Claude Brisson wrote:
> I need to enable only if a type T is among a specific known list.
>
> I tried to build an operator is_one_of< T, list of known types >
> condition that could be used within an enable_if (like others
> type_traits operators), like this:
>
> or< is_same< T, type1 >, or< is_same< T, type2>, ... >
>
> so I searched for an or< condition1, condition2> construct and didn't
> find anything...
>

It's boost::mpl::or_ (Note that or is a keyword)

However, in this case, it's easier to use mpl::set:

#include <boost/mpl/set.hpp>
#include <boost/mpl/has_key.hpp>
#include <boost/utility/enable_if.hpp>

namespace mpl = boost::mpl;

template<class T>
typename boost::enable_if<
    mpl::has_key<mpl::set<int, char, bool>, T>
>::type f(const T&) {}

int main() {
    f(1); // ok
    f("test"); // fails
}

In Christ,
Steven Watanabe


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