在現代C++中面對constexpr修飾符的典型新手問題。struct member和boost :: hana :: filter
我說對了,即使在C++ 17中也沒有辦法編寫這樣的代碼(原因爲http://www.boost.org/doc/libs/1_63_0/libs/hana/doc/html/index.html#tutorial-appendix-constexpr)?
而且必須使用「值作爲類型」成語(在此示例中使用不同類型的打開和關閉文件)?
#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;
}
我真的不知道該代碼的目的是什麼。 –
我有一些結構的constexpr元組,我想在編譯時根據它們的成員字段來獲取它們的一些子集。 – yarrr