mirror of https://github.com/edouarda/brigand.git
7
Primer
Brian Egge edited this page 9 years ago
It all starts with a list:
using my_list = brigand::list<char, int>;
Let's append a type to that list:
using new_list = brigand::push_back<my_list, bool>;
Note that in meta-programming, as the type resulting from the push back is different (and incompatible) with the type before the push back, every operation creates a new, different type. You cannot write something like
using my_list = brigand::push_back<my_list, bool>;
You can get the size of the list
static const std::size_t v = brigand::size<my_list>::value;
What is more interesting is that you can apply algorithms on lists:
// reverse the list
using reversed = brigand::reverse<my_list>;
// transform into a list of pointers
using my_ptr_list = brigand::transform<std::add_pointer<brigand::_1>, my_list>;
Note the use of a MPL style placeholders (brigand::_1) to describe the transformation functor
And you can transform your list into tuples or variants:
brigand::as_tuple<my_list> my_tuple;
brigand::as_variant<my_list> my_variant;
One more thing, brigand also comes up with maps:
using my_map = brigand::map<
brigand::pair<bool, char>,
brigand::pair<int, bool>>;
using lookup_result = brigand::at<my_map, bool>;