命名空间
变体
操作

C++ 关键字: reflexpr (反射 TS)

来自 cppreference.cn
< cpp‎ | 关键字
 
 
C++ 语言
通用主题
流程控制
条件执行语句
if
迭代语句(循环)
for
range-for (C++11)
跳转语句
函数
函数声明
Lambda 函数表达式
inline 说明符
动态异常规范 (直到 C++17*)
noexcept 说明符 (C++11)
异常
命名空间
类型
说明符
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
存储持续性说明符
初始化
 
 

[编辑] 用法

1) 获取类类型的成员列表,或枚举类型的枚举器列表。
2) 获取类型和成员的名称。
3) 检测数据成员是否为 static 或 constexpr。
4) 检测成员函数是否为 virtual、public、protected 或 private。
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
}

这是在反射 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