hana::keys
のdocumentationには、関数呼び出し構文でそれを使用できます。 hana::keys(s)
s
は、概念hana::Struct
を満たすクラスのインスタンスであり、一連のキーオブジェクトを返します。構造のインスタンスなしで `hana :: keys`を使用できますか?
関連する関数hana::accessors
は、構造体のインスタンスから対応するメンバーを取得するために使用できる一連のアクセサ関数を返します。
hana::accessors
can be used in two ways:
hana::accessors(s)
hana::accessors<S>()
はS = decltype(s)
同じものを返すの両方の法的、constexpr
機能です - S
を構造に対応するシーケンス。
hana::keys
でこの構文を試してみると、エラーが発生します。ここでhana
ドキュメントの例から適応MCVE、です:
#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)
を使用する場合には、正常に動作します。
私の実際のアプリケーションでは、構造体のインスタンスがありません。これはテンプレートパラメータに過ぎません。ハックとして
、私はこの作られた:私は、これはドキュメンタリーで説明hana
の実装の詳細の私の限られた理解に基づいて動作することをを信じる
// Work around for `hana::keys`
template <typename S>
constexpr decltype(auto) get_hana_keys() {
return decltype(hana::keys(std::declval<S>())){};
}
を。 - hana::keys
は、コンパイル時の文字列のシーケンスを返すと仮定され、すべての情報が型に含まれているので、型を取得するだけで、それを構成するデフォルトは同等でなければなりません。
MCVEでget_hana_keys<S>()
を使用すると、コンパイルされて正常に動作します。
しかし、それが本当に正しいかどうか、あるいは私が仮定していることが、ドキュメントで想定していることを超えているかどうかはわかりません。
私はブーストバージョン1.61
とgcc 6.2.0
を使用しています。私が知りたいのですがどのような
、
?hana::keys<S>()
が動作しないか、これは単に見落としていることは良い理由があるhana
は非常に細心の注意を払ってデザインされているようです。作成したハックに問題がありますか、改善する方法はありますか?