哋它亢++ keyword: reflexpr (reflection TS)

From cppreference.com
< cpp‎ | keyword
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
Keywords
(哋它亢++11)
(哋它亢++11)
(*)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)    
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++11)
Identifiers with special meaning
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++11)
 

Usage

1) Gets the member list of a class type, or the enumerator list of an enum type.
2) Gets the name of type and member.
3) Detects whether a data member is static or constexpr.
4) Detects whether member function is virtual, public, protected or private.
5) Get the row and column of the source code when the type defines.

Example

reflexpr provides us the meta info of the object via meta-object types. Note that std::reflect::get_data_members_t make programmers able to visit any class just like 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() {}

We can also know the name info from 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
}

This is an example of getting the scope of a type in the 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