Namespaces
Variants

C++ keyword: reflexpr (reflection TS)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

사용법

1) class 타입의 멤버 목록 또는 enum 타입의 열거자 목록을 가져옵니다.
2) 타입 및 멤버의 이름을 가져옵니다.
3) 데이터 멤버가 static 또는 constexpr 인지 감지합니다.
4) 멤버 함수가 virtual , public , protected 또는 private 인지 감지합니다.
5) 타입이 정의될 때 소스 코드의 row column 을 가져옵니다.

예제

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