Hana is a header-only library for C++ metaprogramming suited for computations on both types and values. The functionality it provides is a superset of what is provided by the well established Boost.MPL and Boost.Fusion libraries. By leveraging C++11/14 implementation techniques and idioms, Hana boasts faster compilation times and runtime performance on par or better than previous metaprogramming libraries, while noticeably increasing the level of expressiveness in the process. Hana is easy to extend in a ad-hoc manner and it provides out-of-the-box inter-operation with Boost.Fusion, Boost.MPL and the standard library.
Hana is a header-only library without external dependencies (not even the rest of Boost). Hence, using Hana in your own project is very easy. Basically, just download the project and add the include/ directory to your compiler's header search path and you are done. However, if you want to cleanly install Hana, you have a couple of options:
/usr/local for Unix, C:/Program Files for Windows). If you want to install Hana in a custom location, you can use If you just want to contribute to Hana, you can see how to best setup your environment for development in the README.
If you use CMake, depending on Hana has never been so easy. When installed manually, Hana creates a HanaConfig.cmake file that exports the hana interface library target with all the required settings. All you need is to install Hana manually with CMake, use find_package(Hana), and then link your own targets against the hana target. Here is a minimal example of doing this:
If you have installed Hana in a non-standard place, you might need to play with CMAKE_PREFIX_PATH. For example, this can happen if you "manually" install Hana locally to another project. In this case, you'll need to tell CMake where to find the HanaConfig.cmake file by using
where INSTALLATION_PREFIX_FOR_HANA is the path to the place where Hana was installed.
The library relies on a C++14 compiler and standard library, but nothing else is required. However, we only guarantee support for the compilers listed below, which are tested on an ongoing basis:
| Compiler/Toolchain | Status |
|---|---|
| Clang >= 7 | Fully working; tested on each push to GitHub |
| Xcode >= 11 | Fully working; tested on each push to GitHub |
| GCC >= 8 | Fully working; tested on each push to GitHub |
| VS2017 >= Update 7 | Fully working; tested on each push to GitHub |
More specifically, Hana requires a compiler/standard library supporting the following C++14 features (non-exhaustively):
constexpr<type_traits> headerUsing a compiler not listed above may work, but support for such compilers is not guaranteed. More information for specific platforms is available on the wiki.
If you have a problem, please review the FAQ and the wiki. Searching the issues for your problem is also a good idea. If that doesn't help, feel free to chat with us in Gitter, or open a new issue. StackOverflow with the boost-hana tag is the preferred place to ask questions on usage. If you are encountering what you think is a bug, please open an issue.
When Boost.MPL first appeared, it provided C++ programmers with a huge relief by abstracting tons of template hackery behind a workable interface. This breakthrough greatly contributed to making C++ template metaprogramming more mainstream, and today the discipline is deeply rooted in many serious projects. Recently, C++11 and C++14 brought many major changes to the language, some of which make metaprogramming much easier, while others drastically widen the design space for libraries. A natural question then arises: is it still desirable to have abstractions for metaprogramming, and if so, which ones? After investigating different options like the MPL11, the answer eventually came by itself in the form of a library; Hana. The key insight to Hana is that the manipulation of types and values are nothing but two sides of the same coin. By unifying both concepts, metaprogramming becomes easier and new exciting possibilities open before us.
But to really understand what is Hana all about, it is essential to understand the different types of computations in C++. We will focus our attention on four different kinds of computations, even though a finer grained separation would be possible. First, we have runtime computations, which are the usual computations we use in C++. In that world, we have runtime containers, runtime functions and runtime algorithms:
The usual toolbox for programming within this quadrant is the C++ standard library, which provides reusable algorithms and containers operating at runtime. Since C++11, a second kind of computation is possible: constexpr computations. There, we have constexpr containers, constexpr functions and constexpr algorithms:
std::array's operator== would have to be marked constexpr, which is not the case (even in C++14).Basically, a constexpr computation is different from a runtime computation in that it is simple enough to be evaluated (interpreted, really) by the compiler. In general, any function that does not perform anything too unfriendly to the compiler's evaluator (like throwing or allocating memory) can be marked constexpr without any further change. This makes constexpr computations very similar to runtime computations, except constexpr computations are more restricted and they gain the ability to be evaluated at compile-time. Unfortunately, there is no commonly used toolbox for constexpr-programming, i.e. there is no widely adopted "standard library" for constexpr programming. However, the Sprout library may be worth checking out for those with some interest in constexpr computations.
The third kind of computations are heterogeneous computations. Heterogeneous computations differ from normal computations in that instead of having containers holding homogeneous objects (all objects having the same type), the containers may hold objects with different types. Furthermore, functions in this quadrant of computation are heterogeneous functions, which is a complicated way of talking about template functions. Similarly, we have heterogeneous algorithms that manipulate heterogeneous containers and functions:
If manipulating heterogeneous containers seems overly weird to you, just think of it as glorified std::tuple manipulation. In a C++03 world, the go-to library for doing this kind of computation is Boost.Fusion, which provides several data structures and algorithms to manipulate heterogeneous collections of data. The fourth and last quadrant of computation that we'll be considering here is the quadrant of type-level computations. In this quadrant, we have type-level containers, type-level functions (usually called metafunctions) and type-level algorithms. Here, everything operates on types: containers hold types and metafunctions take types as arguments and return types as results.