ecl_mpl Documentation

ecl_mpl: A library of metaprogramming structures

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


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.


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.



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 {
        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 > {
        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:

#include <ecl/mpl/failed_object.hpp>

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 {
    Unsigned<T>::type my_unsigned_variable;    


Author(s): Daniel Stonier (
autogenerated on Thu Jan 2 2014 11:11:57