documentation对于hana::keys
说我可以在函数调用语法中使用它,例如hana::keys(s)
其中s
是符合概念hana::Struct
的类的一个实例,它返回一系列关键对象。我可以在没有结构实例的情况下使用`hana :: keys`吗?
相关函数hana::accessors
返回可用于从结构实例中获取相应成员的访问函数序列。
hana::accessors
can be used in two ways:
hana::accessors(s)
hana::accessors<S>()
都是合法的,constexpr
函数时S = decltype(s)
返回同样的事情 - 序列对应于结构S
。
当我用hana::keys
尝试此语法时,出现错误。下面是一个MCVE,适应于hana
文件从一个例子:
#include <boost/hana.hpp>
#include <boost/hana/define_struct.hpp>
#include <boost/hana/keys.hpp>
#include <iostream>
#include <string>
namespace hana = boost::hana;
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
// Debug print a single structure
template <typename T>
void debug_print_field(const char * name, const T & value) {
std::cout << "\t" << name << ": " << value << std::endl;
}
template <typename S>
void debug_print(const S & s) {
std::cout << "{\n";
hana::for_each(hana::keys<S>(), [&s] (auto key) {
debug_print_field(hana::to<char const *>(key), hana::at_key(s, key));
});
std::cout << "}" << std::endl;
}
// Debug print compare two structures
int main() {
Person john{"John", 30}, kevin{"Kevin", 20};
debug_print(john);
std::cout << std::endl;
debug_print(kevin);
std::cout << std::endl;
}
$ g++-6 -std=c++14 -I/home/chris/boost/boost_1_61_0/ main.cpp
main.cpp: In function ‘void debug_print(const S&)’:
main.cpp:28:30: error: expected primary-expression before ‘>’ token
hana::for_each(hana::keys<S>(), [&s] (auto key) {
^
main.cpp:28:32: error: expected primary-expression before ‘)’ token
hana::for_each(hana::keys<S>(), [&s] (auto key) {
^
它,当我使用hana::keys(s)
工作正常。
但在我的实际应用中,我没有结构的实例,它只是一个模板参数。
一劈,我做了这个:
// Work around for `hana::keys`
template <typename S>
constexpr decltype(auto) get_hana_keys() {
return decltype(hana::keys(std::declval<S>())){};
}
我相信这个作品基于我有限的实况描述hana
实现细节的理解。 - hana::keys
应该返回一个编译时字符串序列,并且所有信息都包含在该类型中,所以获取类型和默认构造它应该是等效的。
当我在我的MCVE中使用get_hana_keys<S>()
时,它编译并运行正常。
但是我不知道它是否真的正确,或者我所做的假设超出了文档允许我假设的范围。
我正在使用增强版本1.61
和gcc 6.2.0
。
我想知道的是什么,
是否有一个很好的理由,
hana::keys<S>()
不工作或者是这只是一个疏忽?hana
似乎已经非常精心设计,所以我倾向于在这里猜测自己。我创建的破解或改进方法有什么问题吗?