Boost.Dispatch is a Boost candidate library that aims at providing an idiomatic way to use tag dispatching in a way functions can be extended or specialized, with each specialization being able to be generic with the minimal amount of work required. With tag dispatching, it becomes then possible to select the best specialization for the arguments provided, avoiding the limitations inherent to unconstrained template function overloads.
Boost.Dispatch solves these problems through two related components:
-
An extensible hierarchy system that contains ready-made hierarchies and that can be easily extended. This provides a powerful and idiomatic way to define tags embedding arbitrary level of intentionality.
-
A dispatching mechanism built on top of the hierarchies and overloading, that automates the use of tag dispatching with one or all arguments.
Fundamental and standard types are automatically hierarchized using a recursive, bottom-up descriptive set of hierarchy tags that allow fine to coarse grain type discrimination. A sample tag-dispatched function can then be written like:
#include <boost/dispatch/hierarchy_of.hpp>
#include <boost/dispatch/hierarchy.hpp>
using namespace boost::dispatch;
// C call for float
template<typename T> T absolute_value(T t, scalar_<single_<T>> const&)
{
return ::fabs(t);
}
// C call for double
template<typename T> T absolute_value(T t, scalar_<double_<T>> const&)
{
return ::abs(t);
}
// Regular abs implementation for integers
template<typename T> T absolute_value(T t, scalar_<integer_<T>> const&)
{
return t < 0 ? -t : t;
}
// abs is identity or unsigned value
template<typename T> T absolute_value(T t, scalar_<unsigned_<T>> const&)
{
return t;
}
// User-level function
template<typename T> T absolute_value(T t)
{
return absolute_value(t, hierarchy_of(t) );
}
COMING SOON
- Repository : https://github.com/numscale/boost.dispatch
- Documentation : http://numscale.github.io/boost.dispatch
Platform & Compiler | master |
develop |
---|---|---|
Linux clang & g++ | ||
OS X clang |