Alleviate long qualifications in header files

Issue

I define an inline function object in a header file, like this:

// fmap.hpp
namespace util {
    inline auto constexpr fmap = boost::hana::curry<2>(boost::hana::flip(boost::hana::transform));
}

Client code can simply #include "fmap.hpp" and start using util::fmap as they like.

So far so good.

But sometimes the definition of such objects can be cumbersome to read, if they are so full of qui::quo::qua::lify.

How can I alleviate this?

Ideally, I’d like the definition of fmap to look like this:

namespace util {
    inline auto constexpr fmap = curry<2>(flip(transform));
}

but at the same time I don’t want to put a using namespace boost::hana; at top level, as client code’s namespace would be cluttered with boost::hana (or from other namespaces) names, not to mention that some compilers have hard time with using namespace directives in generic lambdas.

Is there some C++ guideline or best practice that comes handy in this situation?

Solution

Thinking about it, I can solve the riddle by just constructing and calling on the fly a lambda from void (hence the () between [] and {} can be removed) to the desired lambda:

namespace utils {

inline auto constexpr fmap = []{
    using namespace boost::hana;
    return curry<2>(flip(transform));
}();

}

Answered By – Enlico

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.