views:

134

answers:

4

Basically, I am looking for a library solution that does this:

#include <boost/type_traits.hpp>

template<bool>
struct bool_to_bool_type;

template<>
struct bool_to_bool_type<false>
{
    typedef boost::false_type type;
};

template<>
struct bool_to_bool_type<true>
{
    typedef boost::true_type type;
};

Is there such a metafunction?

+1  A: 

Boost already provides boost::mpl::bool_<>:

Boost.TypeTraits uses integral_constant:

BOOST_STATIC_ASSERT(( 
    is_same< integral_constant<bool, false>, 
             is_class<int>::type >::value ));
Georg Fritzsche
+1  A: 

Oh wait, true_type is just a typedef for std::integral_constant<bool, true>? Then there's an obvious solution:

boost::integral_constant<bool, input_value>
FredOverflow
That's it. :) Though surely you mean `boost::integral_constant`, not `std::`.
GMan
Yeah, I always confuse `boost` and `std` and `std::tr1` namespaces... :)
FredOverflow
Fred, `integral_constant` it is - thats what i was trying to get across in my answer. Maybe you could read the answers first? :p
Georg Fritzsche
+1  A: 

While integral_constant is the answer, you may make a type or two just to make things a bit clearer. I use this in my library:

// utility/bool_type.hpp
#include <boost/type_traits/integral_constant.hpp>

namespace utility
{
    template <bool B>
    struct bool_type : boost::integral_constant<bool, B>
    {
        static const bool value = B;
    };

    typedef const boost::true_type& true_tag;
    typedef const boost::false_type& false_tag;
}

// main.cpp
// just some predicate for example
template <typename T>
struct is_pointer
{
    static const bool value = false;
};

template <typename T>
struct is_pointer<T*>
{
    static const bool value = true;
};

// some specialized function.
// the true/false tag parameter is a
// little easier to read, i think
template <typename T>
void foo(T, utility::true_tag)
{
}

/* versus:
template <typename T>
void foo(T, const boost::true_type&)
*/

template <typename T>
void foo(T, utility::false_tag)
{
}

// the actual function
template <typename T>
void foo(T pX)
{
    // a bit shorter
    foo(pX, utility::bool_type<is_pointer<T>::value>());

    /* versus:
    foo(pX, boost::integral_type<bool, is_pointer<T>::value>());
    */
}

int main(void)
{
    int i = 0;
    foo(i);
    foo(&i);
}

But that's just my opinion. :]

GMan
A: 

GMan there is a much better way to do what you're doing, just make your predicate meta-functions derive from true/false_type (those are type aliases for integral_constant) e.g.:

#include <type_traits>

template <typename T>
struct is_pointer : std::false_type {};

template <typename T>
struct is_pointer<T*> : std::true_type {};

//....
foo(px, is_pointer<T>());

Note: I'm using C++0x library.

This whole idea of bool -> bool_type meta-functions are redundant when you have meta-data wrappers like integral_type, just look at the definition of it closely to see what I'm talking about and it's been designed to work like my example.

snk_kid
You're missing the point of my post, I'm afraid. My predicate was only for example. The `bool` itself might not even come from such a predicate, as the OP demonstrates.
GMan