【问题标题】:Alleviate long qualifications in header files减轻头文件中的长限定
【发布时间】:2021-09-13 06:26:34
【问题描述】:

我在头文件中定义了一个inline函数对象,像这样:

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

客户端代码可以简单地#include "fmap.hpp" 并开始使用他们喜欢的util::fmap

到目前为止一切顺利。

但有时这些对象的定义可能会很麻烦,如果它们充满了qui::quo::qua::lify

我该如何缓解这种情况?

理想情况下,我希望fmap 的定义如下所示:

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

但同时我不想将using namespace boost::hana; 放在顶层,作为client code's namespace would be cluttered with boost::hana(或来自其他命名空间)的名称,更不用说some compilers have hard time with using namespace directives in generic lambdas

在这种情况下是否有一些 C++ 指南或最佳实践可以派上用场?

【问题讨论】:

  • 如果你不想弄乱你的全局或util命名空间,你可以创建一个嵌套命名空间util::helper(或类似的),你可以是using namespace whatever;并声明所有类型帮助器类型,然后由 util 命名空间中的符号使用。
  • @Someprogrammerdude,是的,我不想弄乱 [...]。但我没有完全理解你的建议。我是否必须将fmap 声明也放入helper,然后回到util 我会做using helper::fmap 以便客户看到util::fmap
  • 你的定义没有错。它很简单,也很容易理解。也许只是把它分成两行。
  • 这是一个意见。对我来说,理解所有::s 写的内容需要更长的时间。

标签: c++ namespaces c++17 header-files using


【解决方案1】:

考虑一下,我可以通过从void(因此[]{} 之间的() 可以删除)到所需的lambda 来构建和即时调用一个lambda:

namespace utils {

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

}

【讨论】:

    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 2010-11-26
    • 2012-07-11
    • 1970-01-01
    • 2012-02-01
    • 2021-06-30
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多