减轻头文件中的长限制 [英] Alleviate long qualifications in header files
问题描述
我在头文件中定义了一个 inline
函数对象,如下所示:
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));
}
客户端代码可以简单地#include "fmap.hpp"
并开始使用 util::fmap
.
Client code can simply #include "fmap.hpp"
and start using util::fmap
as they like.
到目前为止一切顺利.
但有时这些对象的定义读起来很麻烦,如果它们充满了qui::quo::qua::lify
.
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?
理想情况下,我希望 fmap
的定义如下所示:
Ideally, I'd like the definition of fmap
to look like this:
namespace util {
inline auto constexpr fmap = curry<2>(flip(transform));
}
但同时我不想把 using namespace boost::hana;
放在顶层,因为客户端代码的命名空间会被 boost::hana
(或来自其他命名空间)名称.
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.
在这种情况下,是否有一些 C++ 指南或最佳实践可以派上用场?
Is there some C++ guideline or best practice that comes handy in this situation?
推荐答案
我的其他自我回答很好,但它也创建了一个虚假的用户可见类 Fmap
,我真的不需要存在.
My other self answer is good, but it too creates a spurious and user-visible class Fmap
which I really don't need to exist.
考虑一下,我可以通过构建并动态调用从 void
到所需 lambda 的 lambda 来解决这个谜语:
Thinking about it, I can solve the riddle by just constructing and calling on the fly a lambda from void
to the desired lambda:
namespace utils {
inline auto constexpr fmap = [](){
using namespace boost::hana;
return curry<2>(flip(transform));
}();
}
这篇关于减轻头文件中的长限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!