BOOST_HOF_STATIC_FUNCTION

Description

The BOOST_HOF_STATIC_FUNCTION macro allows initializing a function object from a constexpr expression. It uses the best practices as outlined in N4381. This includes using const to avoid global state, compile-time initialization of the function object to avoid the static initialization order fiasco, and an external address of the function object that is the same across translation units to avoid possible One-Definition-Rule(ODR) violations.

In C++17, this achieved using the inline keyword. However, on older compilers it is initialized using a reference to a static member variable. The static member variable is default constructed, as such the user variable is always default constructed regardless of the expression.

By default, all functions defined with BOOST_HOF_STATIC_FUNCTION use the reveal adaptor to improve error messages.

Example

#include <boost/hof.hpp>
#include <cassert>

struct sum_f
{
    template<class T, class U>
    T operator()(T x, U y) const
    {
        return x+y;
    }
};

BOOST_HOF_STATIC_FUNCTION(sum) = sum_f();
BOOST_HOF_STATIC_FUNCTION(partial_sum) = boost::hof::partial(sum_f());

int main() {
    assert(sum(1, 2) == partial_sum(1)(2));
}