0

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?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    If you don't want to clutter up your global or `util` namespaces, you could create a nested namespace `util::helper` (or some such) where you could be `using namespace whatever;` and declare all kinds of helper types, which are then used by the symbols in the `util` namespace. – Some programmer dude Sep 13 '21 at 06:34
  • @Someprogrammerdude, yes, I don't want to clutter [...]. But I haven't understood fully your suggestion. Would I have to put `fmap` declaration too into `helper` and then, back in `util` I'd do `using helper::fmap` so that clients would see `util::fmap`? – Enlico Sep 13 '21 at 07:25
  • There is nothing wrong with your definition. It is simple, and easy to understand. Maybe just split it into two lines. – TonyK Oct 01 '21 at 14:46
  • That's an opinion. For me it takes longer to understand what's written with all those `::`s. – Enlico Oct 01 '21 at 14:49

1 Answers1

0

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));
}();

}
Enlico
  • 23,259
  • 6
  • 48
  • 102