close
close

Programmer’s language: Reflection in C++26 | heise online

Programmer’s language: Reflection in C++26 | heise online

Reflection of Deutsch Reflexion is the quality of a program, the structure and the presentation of things, of relationships and of things.

Display




Reflection in C++ is more. Here are two examples of the proposal (P2996R5).




Rainer Grimm is often busy with software architecture, team and debt counselling. A new article has been written about the programs of C++, Python and Haskell, which speak about a new and higher level of fakconferenzen. On his Blog Modern C++ is intensive with the Leidenschaft C++.

We don’t just want to observe the structure of the program: we also want to simplify the generation of code that depends on those observations. That combination is sometimes called “reflective metaprogramming”, but within the WG21 discussion the term “reflection” has often been used informally to refer to the same general idea.

This proposal is not intended to be an endgame as far as reflection and compile-time metaprogramming are concerned. Instead, we expect it to be a useful kernel to which more powerful features will be added incrementally over time. In particular, we believe that most or all of the remaining features explored in P1240R2 and that code injection (along the lines described in (P2237R0)) are desirable directions to pursue.

The representation of Reflection in C++ is based on the template metaprogramming. The template metaprogramming started in 1994 with the reflection. C++98 offers the Laufzeit-Reflection (RTTI) and the Functions-Template-Type-Derivation. The Data-Type-Library in C++11 works with the high quality of C++. In C++26 a general Reflection-Unterstützung is implemented.

I came across the Vorstellung van Reflection in C++26 on the proposal P2996R5 and played from it.

It could be that the Reflection Value is reflected in the grammatical elements before and through.

The following program starts in the grammatical area, jumps into the reflection area and continues into the grammatical area.

// forthAndBack.cpp (P2996R5)

#include 
#include 
#include 

int main() {
    constexpr auto r = ^int;
    typename(:r:) x = 42;       // Same as: int x = 42;
    typename(:^char:) c = '*';  // Same as: char c = '*';

    static_assert(std::same_as);
    static_assert(std::same_as);
    assert(x == 42);
    assert(c == '*');
}

  • ^: Reflexionsoperator erzeugt een Reflexionswert aus seinem Operanden (^int and ^char)
  • (: refl :): Splicer indicates a grammatical element from a reflection word ((:r:) and (:^char:))
  • Reflexionswert is a Darstellung von Programmalementen als constant Ausdruck

The belch ^gramOper shows a Reflection with a Date Type: std::meta::info. std::same_as is a concept.

The Jumping of the Reach Grammar and Reflection is all that is a sin. Here you can perform a program analysis enumString.cpp:

Enum => String

The following hiking trail is an Enum-Wert in a String um:

// enumString.cpp

#include 
#include 
#include 
#include 


template
  requires std::is_enum_v                      // (1)
constexpr std::string enum_to_string(E value) {
  std::string result = "";
  (:expand(std::meta::enumerators_of(^E)):) >>    // (2)
  (&){
    if (value == (:e:)) {
      result = std::meta::identifier_of(e);       // (3)
    }
  };
  return result;
}


int main() {

    std::cout 

In (1) mithilfe der Datentyp-Eigenschaft std::is_enum checked, ob value an Enumerator ist. Der Ausdruck ^E in (2) erzeugt den Reflexionswert. The function expand in derselben Zeile darf man ignores. The expansion possibilities are active in the active implementation.

The functions std::meta::enumerators_of and std::meta::enumerators_of in (2) and (3) metafunctions. If you look at the compilation page now, this is the case consteval have been declared.

Here are some metafunctions.

namespace std::meta {
  consteval auto members_of(info type_class) -> vector;
  consteval auto bases_of(info type_class) -> vector;

  consteval auto static_data_members_of(info type_class) -> vector;
  consteval auto nonstatic_data_members_of(info type_class) -> vector;

  consteval auto subobjects_of(info type_class) -> vector {
    auto subobjects = bases_of(type_class);
    subobjects.append_range(nonstatic_data_members_of(type_class));
    return subobjects;
  }

  consteval auto enumerators_of(info type_enum) -> vector;
}

All meta functions are a fact std::vector back. Reflection offers many meta-functions, a number of data types that can be analyzed and coded.

The application of the written words from string to enum:

template 
  requires std::is_enum_v                           
constexpr std::optional string_to_enum(std::string_view name) {
  template for (constexpr auto e : std::meta::enumerators_of(^E)) {
    if (name == std::meta::identifier_of(e)) {                     
      return (:e:);
    }
  }

  return std::nullopt;
}

Reflection offers many meta-functions. I was expected in my next article.


(I)