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:
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:
template <typename T>
template <>
class is_foo_bar< Tango > :
public ecl::True {};
template <bool B = false>
class Cash {
static void says() {
std::cout << "Cash is fine." << std::endl;
}
};
template <>
class Cash <true> {
static void says() {
std::cout << "Cash is foobar too." << std::endl;
}
};
int main() {
Cash<is_foo_bar<Tango>::value>::says();
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.
template <typename T, typename Enable = void>
class TestObject {
public:
bool isFloatSpecialisation() { return false; }
};
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>
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.