C++ キーワード: reflexpr (reflection TS)
From cppreference.com
[編集] 使用法
2) 型とメンバーの名前を取得します。
5) 型が定義されているソースコードの行と列を取得します。
[編集] 例
reflexpr はメタオブジェクト型を介してオブジェクトのメタ情報を提供します。std::reflect::get_data_members_t はプログラマが std::tuple のように任意のクラスを訪問できるようにすることに注意してください。
このコードを実行
#include <string> #include <vector> struct S { int b; std::string s; std::vector<std::string> v; }; // Reflection TS #include <experimental/reflect> using meta_S = reflexpr(S); using mem = std::reflect::get_data_members_t<meta_S>; using meta = std::reflect::get_data_members_t<mem>; static_assert(std::reflect::is_public_v<meta>); // successful int main() {}
reflexpr から名前情報も知ることができます
このコードを実行
#include <iostream> #include <string> #include <string_view> // Reflection TS #include <experimental/reflect> template<typename Tp> constexpr std::string_view nameof() { using TpInfo = reflexpr(Tp); using aliased_Info = std::experimental::reflect::get_aliased_t<TpInfo>; return std::experimental::reflect::get_name_v<aliased_Info>; } int main() { std::cout << nameof<std::string>() << '\n'; static_assert(nameof<std::string>() == "basic_string"); // successful }
これは、Reflection TS で型のスコープを取得する例です。
このコードを実行
namespace Foo { struct FooFoo { int FooFooFoo; }; } namespace Bar { using BarBar = ::Foo::FooFoo; } using BarBarInfo = reflexpr(::Bar::BarBar); using BarBarScope = ::std::experimental::reflect::get_scope_t<BarBarInfo>; // Bar, not Foo struct Spam { int SpamSpam; }; struct Grok { using GrokGrok = Spam::SpamSpam; }; using GrokGrokInfo = reflexpr(::Grok::GrokGrok); using GrokGrokScope = std::experimental::reflect::get_scope_t<GrokGrokInfo>; // Grok, not Spam