结构成员和 boost::hana::filter

struct member and boost::hana::filter

遇到了现代 c++ 中 constexpr 修饰符的典型新手问题。

即使在 c++17 中也无法编写这样的代码,我说得对吗(http://www.boost.org/doc/libs/1_63_0/libs/hana/doc/html/index.html#tutorial-appendix-constexpr 的原因)?

并且必须使用 "value as a type" 习语(在本例中对打开和关闭的文件使用不同的类型)?

#include <boost/hana/filter.hpp>
#include <boost/hana/tuple.hpp>

namespace hana = boost::hana;

enum class State {
  Open, Closed
};

struct File {
  constexpr File(State state) : state_(state) {}

  constexpr State state() const { return state_; }

  const State state_;
};

constexpr auto files = hana::make_tuple(File(State::Closed), File(State::Open));
constexpr auto filtered = hana::filter(files, [](const auto& file) {
  return file.state() == State::Open;
});

int main() {
  return 0;
}

您需要将 State 编码为 File 类型的一部分:

template <State TState>
struct File 
{
    constexpr State state() const { return TState; }
};

然后,您需要 return 来自过滤函数的编译时友好布尔值:

constexpr auto files = hana::make_tuple(File<State::Closed>{}, File<State::Open>{});
constexpr auto filtered = hana::filter(files, [](const auto& file) 
{
    return hana::bool_c<decltype(file){}.state() == State::Open>;
});

live example on wandbox

在您传递给 hana::filter 的 lambda 中,您有两个问题:

  1. file参数不是constexpr
  2. return 值不是 hana::filter 所要求的可转换为 bool 的编译时 IntegralConstant

在任何上下文中保持元组元素 constexpr 的一种方法是将它们包装在 constexpr lambda 中(在 C++17 中添加)。

以下代码解决了这两个问题:

constexpr auto files = hana::make_tuple(
  []{ return File(State::Closed); }
, []{ return File(State::Open); }
);

constexpr auto filtered = hana::filter(files, [](auto file) {
  return hana::bool_c<file().state() == State::Open>;
});