Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Boolean Logic
Description: Using compile time boolean types for metaprogramming.Tutorial Level: INTERMEDIATE
Overview
It provides a compile-time boolean type used to make logical inferences in the template parameters supplied to template classes. It is often used in the is_such_and_such_class traits testing classes (there are some in ecl_traits).
Usage
1 #include <ecl/mpl/bool.hpp>
2
3 // Parent template which defaults to false
4 template <typename T>
5 class is_foo_bar : public ecl::False {};
6
7 // Specialisations (in this case, class Tango) for which it is true
8 template <>
9 class is_foo_bar< Tango > : public ecl::True {};
10
11 // A class that will use it
12 template <bool B = false>
13 class Cash {
14 static void says() {
15 std::cout << "Cash is fine." << std::endl;
16 }
17 };
18
19 // specialisation of that class for true
20 template <>
21 class Cash <true> {
22 static void says() {
23 std::cout << "Cash is foobar too." << std::endl;
24 }
25 };
26
27 int main() {
28 Cash<is_foo_bar<Tango>::value>::says(); // Cash is foobar too...
29 return 0;
30 }