Created
November 11, 2012 21:50
-
-
Save bo0ts/4056392 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/mpl/vector/vector10.hpp> | |
#include <boost/mpl/if.hpp> | |
#include <boost/mpl/at.hpp> | |
#include <boost/type_traits/is_integral.hpp> | |
#include <boost/type_traits/is_reference.hpp> | |
#include <boost/type_traits/is_same.hpp> | |
#include <boost/static_assert.hpp> | |
#include <iostream> | |
struct choice_a { | |
void operator()() const { std::cout << "a" << std::endl; } | |
}; | |
struct choice_b { | |
void operator()() const { std::cout << "b" << std::endl; } | |
}; | |
struct choice_c { | |
void operator()() const { std::cout << "c" << std::endl; } | |
}; | |
typedef boost::mpl::vector3<choice_a, choice_b, choice_c> choices; | |
// \returns 0 when T is integral, 1 when T is a reference and 2 for | |
// everything else. Note | |
template <typename T> | |
struct make_choice | |
: boost::mpl::eval_if< typename boost::is_integral<T>::type | |
, boost::mpl::int_<0> | |
, boost::mpl::eval_if< typename boost::is_reference<T>::type | |
, boost::mpl::int_<1> | |
, boost::mpl::int_<2> | |
> | |
> | |
{}; | |
struct X {}; | |
int main() | |
{ | |
//print<make_choice<int&>::type> adsf; | |
BOOST_STATIC_ASSERT((boost::is_same< make_choice<long>::type, boost::mpl::int_<0> >::value)); | |
// int& is not an integral | |
BOOST_STATIC_ASSERT((boost::is_same< make_choice<int&>::type, boost::mpl::int_<1> >::value)); | |
// const int is | |
BOOST_STATIC_ASSERT((boost::is_same< make_choice<const int>::type, boost::mpl::int_<0> >::value)); | |
// the rest | |
BOOST_STATIC_ASSERT((boost::is_same< make_choice<double>::type, boost::mpl::int_<2> >::value)); | |
BOOST_STATIC_ASSERT((boost::is_same< make_choice<X>::type, boost::mpl::int_<2> >::value)); | |
// now actually select a functor and call it based on our choice | |
boost::mpl::at< choices, make_choice<long>::type >::type()(); // a | |
boost::mpl::at< choices, make_choice<X&>::type >::type()(); // b | |
boost::mpl::at< choices, make_choice<double>::type >::type()(); // c | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment