Boost.Hana
Heterogeneous combiNAtors for expressive metaprogramming
Disclaimers
This is not an official Boost library. However, a formal review will be asked for shortly. The library is fairly stable, but no effort will be made to stay backward compatible.
Overview
#include <boost/hana/assert.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <string>
using namespace boost::hana;
struct President { std::string name; };
struct Car { std::string name; };
struct City { std::string name; };
int main() {
// Heterogeneous sequences for value-level metaprogramming.
auto stuff = tuple(President{"Obama"}, Car{"Toyota"}, City{"Quebec"});
auto names = transform(stuff, [](auto thing) { return thing.name; });
BOOST_HANA_RUNTIME_CHECK(reverse(names) == tuple("Quebec", "Toyota", "Obama"));
// No compile-time information is lost:
// `stuff` wasn't constexpr but its length is!
static_assert(length(stuff) == 3u, "");
// Type-level metaprogramming works too.
auto types = transform(stuff, [](auto thing) {
return type<decltype(thing)*>;
});
BOOST_HANA_CONSTANT_CHECK(
types == tuple(type<President*>, type<Car*>, type<City*>)
);
}
Prerequisites and installation
Boost.Hana is a header only library. To use it in your own project, just add the include directory to your compiler's header search path and you are done.
The library relies on a full-featured C++14 compiler and standard library, but nothing else is required. As of November 2014, the only compiler known to compile the full test suite is Clang 3.6.0 (trunk) with libc++ (trunk too). While Clang 3.5 is advertised as having full support for C++14, it has several C++14-related bugs that are fixed in the trunk and make it incapable of compiling the full test suite. However, efforts were made to make the library usable with Clang 3.5 and the libc++ that's shipped with it; you should expect a couple of compiler bugs to pop up here and there, but most functionality should be available.
Documentation
You can browse the documentation online at http://ldionne.github.io/hana.
You can also get an offline version of the documentation by checking out
the doc/gh-pages submodule at its latest version:
git submodule update --init --remote
Hacking on Hana
Setting yourself up to work on Boost.Hana is easy. First, you will need an
installation of CMake. Once this is done, you can cd to the root of
the project and setup the build directory:
mkdir build
cd build
cmake ..
You can now build and run the unit tests and the examples. I assume that you used the Makefile generator with CMake; the commands may differ for other generators:
make tests
make examples
Tip
There is a Makefile at the root of the project which forwards everything to the
builddirectory. Hence, you can also issue those commands from the root of the project instead of thebuilddirectory.
There are also optional targets which are enabled only when the required software is available on your computer. For example, generating the documentation requires Doxygen to be installed. An informative message will be printed during the CMake generation step whenever an optional target is disabled. You can install any missing software and then re-run the CMake generation to update the list of available targets.
Tip
You can use the
helptarget to get a list of all the available targets.
If you want to add unit tests or examples, just add a source file in test/
or example/ and then re-run the CMake generation step so the new source
file is known to the build system. If the relative path from the root of
the project to the new source file is path/to/file.cpp, a target named
path.to.file to compile the file will be created when CMake is run.
Tip for Sublime Text users
If you use the provided hana.sublime-project file, you can select the "Build current file" build system to build the target associated to the current file.
Project organization
The project is organized in a couple of subdirectories.
- The benchmark directory contains compile-time and runtime benchmarks to make sure the library is as fast as advertised. The benchmark code is written mostly in the form of eRuby templates. The templates are used to generate C++ files which are then compiled while gathering compilation statistics. The benchmarks are driven by CMake files. Note that currently the benchmarks will only work with Clang because of the gem used to drive the compiler and gather the statistics.
- The cmake directory contains additional CMake modules used by the build system.
- The doc directory contains configuration files needed to generate
the documentation. Also, the doc/gh-pages directory is
a submodule tracking the
gh-pagesbranch of the official repository at http://github.com/ldionne/hana, which contains the latest documentation. - The example directory contains the source code for all the examples of both the tutorial and the reference documentation.
- The include directory contains the library itself, which is header only.
- The test directory contains the source code for all the unit tests.
Contributing
Want to contribute? Great!
- Fork it.
- Create a branch (
git checkout -b feature_X) - Do your modifications on that branch
- Make sure you did not break anything (
make tests examples) - Commit your changes (
git commit -am "Added feature X") - Push to the branch (
git push origin feature_X) - Open a pull request
Related material
I presented a talk on Hana at CppCon 2014. If you are interested in metaprogramming, you may also take a look at the MPL11, which is how Hana started out, and this talk about the MPL11 at C++Now 2014.
License
Please see LICENSE.md.