Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ keyword:reflexpr(reflection TS)

      From cppreference.com
      <cpp‎ |keyword
       
       
      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
       
      Keywords
      Identifiers with special meaning
      (C++11)
      (C++20)
      (C++20)
      (C++11)
      (C++26)
      (C++26)
       
      Experimental Feature The functionality described on this page is part of the Reflection Technical Specification ISO/IEC TS 23619(reflection TS).

      [edit]Usage

      1) Gets the member list of aclass type, or the enumerator list of anenum type.
      2) Gets the name of type and member.
      3) Detects whether a data member isstatic orconstexpr.
      4) Detects whether member function isvirtual,public,protected orprivate.
      5) Get therow andcolumn of the source code when the type defines.

      [edit]Example

      reflexpr provides us the meta info of the object viameta-object types. Note thatstd::reflect::get_data_members_t make programmers able to visit any class just likestd::tuple.

      Run this code
      #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 fromreflexpr:

      Run this code
      #include <iostream>#include <string>#include <string_view>// Reflection TS#include <experimental/reflect> template<typename Tp>constexprstd::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 thescope of a type in theReflection TS.

      Run this code
      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
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/reflexpr&oldid=175062"

      [8]ページ先頭

      ©2009-2025 Movatter.jp