|
Boost : |
From: Korcan Hussein (korcan_h_at_[hidden])
Date: 2006-03-17 08:33:16
Enumerated Types (& simulated pattern matching):
#include <iostream>
#include <boost/mpl/size_t.hpp>
#include <boost/variant.hpp>
namespace mpl = boost::mpl;
struct APPLES_ : mpl::size_t<0u> {} const APPLES;
struct ORANGES_ : mpl::size_t<1u> {} const ORANGES;
struct PEACHES_ : mpl::size_t<2u> {} const PEACHES;
typedef boost::variant<APPLES_, ORANGES_, PEACHES_> fruit_type;
struct fruit_printer : boost::static_visitor<void> {
void operator()(APPLES_ a) const {
std::cout << "apples: " << a << '\n';
}
template < typename Tp >
void operator()(Tp v) const { // catch all
std::cout << typeid(Tp).name() << ": " << v << '\n';
}
} const print_fruit;
int main() {
fruit_type my_fruit = APPLES;
boost::apply_visitor(print_fruit, my_fruit);
my_fruit = ORANGES;
boost::apply_visitor(print_fruit, my_fruit);
my_fruit = PEACHES;
boost::apply_visitor(print_fruit, my_fruit);
// my_fruit = mpl::size_t<4u>(); // compile-time error
// my_fruit = 6u; // compile-time error
}
There might be some merit here such that generalization is applicable with
some macro metaprogramming.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk