ecl_mpl Documentation

ecl_mpl

Metaprogramming tools move alot of runtime calculations to be shifted to compile time. This has only very elementary structures at this stage.

Embedded Control Library

At this point, this package only provides very elementary support to the other ecl template classes. Alot of ideas for these elements come from both boost and the book "C++ Template Metaprogramming" by D.Abrahams.

Compiling & Linking

Include the following at the top of any translation unit:

#include <ecl/mpl.hpp>
// The concept definition/validation classes.
using ecl::Bool; // compile time logic testing of template parameters
using ecl::enable_if; // selective specialisation of classes via the template parameters
using ecl::Unsigned; // type converters

Since it is a collection of template classes, no linking is required.

Usage

Bool

Often used in the is_such_and_such_class traits testing classes. For example:

#include <ecl/mpl/bool.hpp>
// Parent template which defaults to false
template <typename T>
class is_foo_bar : public ecl::False {};
// Specialisations (in this case, class Tango) for which it is true
template <>
class is_foo_bar< Tango > : public ecl::True {};
// A class that will use it
template <bool B = false>
class Cash {
static void says() {
std::cout << "Cash is fine." << std::endl;
}
};
// specialisation of that class for true
template <>
class Cash <true> {
static void says() {
std::cout << "Cash is foobar too." << std::endl;
}
};
int main() {
Cash<is_foo_bar<Tango>::value>::says(); // Cash is foobar too...
return 0;
}

Enable If

These allow you to various compile tricks with SFINAE (Substitution Failure Is Not An Error). One usage case is to specialise a template class for a whole group of classes that register with a common type trait.

// This will instantiate if it is anything except float or double.
template <typename T, typename Enable = void>
class TestObject {
public:
bool isFloatSpecialisation() { return false; }
};
// This specialisation will instantiate it T is float or double.
template <typename T>
class TestObject< T, typename enable_if< is_float<T> >::type > {
public:
bool isFloatSpecialisation() { return true; }
};

Failed Object

Simple class that guarantees a compile time failure if an attempt to instantiate it is made (private constructor). Commonly you would use with the mpl if function.

For example, the parent template definition above could be instead:

template <typename T, typename Enable = void>
class TestObject : public ecl::FailedObject {};

Compile Time Converters

Convert from one type to another in compile time.

char c = 0x33;
Unsigned<char>::type uc = 0x03;

That example is trivial, but is more useful when using it for template arguments in a class.

template <typename T>
class Foo {
private:
Unsigned<T>::type my_unsigned_variable;
};

ChangeLog

  • Feb 10 : moved out type traits to their own package.
  • Aug 10 : FailedObject to throw compile time failures when metaprogramming.
  • Jul 10 : enable_if added until c++0x standards become mainstream.
  • Jul 09 : Bool, If constructs added.
bool.hpp
Defines the metaprogamming equivalent of the boolean type.
ecl::Unsigned
Primary template for signed to unsigned type metafunctions.
Definition: converters.hpp:30
mpl.hpp
Metaprogramming (pseudo) library.
ecl::Bool
Integral constant wrapper for boolean values.
Definition: bool.hpp:36
ecl::enable_if
Enables the SFINAE concept.
Definition: enable_if.hpp:71
ecl::FailedObject
An object designed to fail with compile time error upon instantiation.
Definition: failed_object.hpp:28
failed_object.hpp
An object to use when you want to throw a compile time failure.


ecl_mpl
Author(s): Daniel Stonier
autogenerated on Sun Aug 2 2020 03:12:05