std::meta::is_accessible

来自cppreference.com
< cpp | meta
 
 
 
反射库
 
反射类型与查询
反射查询
反射布局查询
类型属性
类型属性查询
 
在标头 <meta> 定义
consteval bool is_accessible( std::meta::info r, std::meta::access_context ctx );
(C++26 起)

判断 r 所表示的成员或基类在 ctx 的上下文中是否可访问

如果 r 不表示类成员或直接基类关系,则返回 true。否则,设:

  • PARENT_CLS 为包含 r 所表示事物的声明的类,或直接基类关系中的派生类;
  • DESIGNATING_CLS 为:如果 ctx.designating_class() 不是空反射,则为其值,否则为 PARENT_CLS

结果按如下方式确定:

  • 如果 DESIGNATING_CLSPARENT_CLS 既不相同也不是其派生类,则返回 false
  • 否则,如果 ctx.scope() 是空反射,则返回 true
  • 否则,如果 r 表示一个基类为 B 的直接基类关系,则:如果 DESIGNATING_CLS 的基类 Bctx.scope() 内可访问,则返回 true;否则返回 false
  • 否则,r 表示一个成员 M。如果忽略任何using 声明的影响,M 作为 DESIGNATING_CLS 的成员在 ctx.scope() 内可访问,则返回 true;否则返回 false

该函数将未命名位域视为具有相同访问权限的非静态数据成员。

参数

r - 一个反射值
ctx - 一个访问上下文

返回值

如果 r 所表示的事物在上下文 ctx 中可访问,则返回 true,否则返回 false

异常

如果 r 表示当前正在定义的类的成员,则抛出 std::meta::exception

示例

#include <meta>

consteval std::meta::access_context fn()
{
    return std::meta::access_context::current();
}

class C
{
    [[maybe_unused]] int moo;
    friend consteval std::meta::access_context fn();

public:
    static constexpr auto r{^^moo};
};

static_assert(is_accessible(C::r, fn()) == true);
static_assert(is_accessible(C::r, std::meta::access_context::current()) == false);
static_assert(is_accessible(C::r, std::meta::access_context::unchecked()) == true);

int main() {}

参阅

表示访问检查的上下文
(类) [编辑]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.