Welcome to v1.0 of the meta::[[verse]]!
# Slide 1# Slide 2https://slides.com/inballevi/welcome-to-v1-0-of-the-meta-verse-pure-virtual/
# Slide 3# Slide 4(*) Examples from or derive from P2996, or EDG's implementation
# Slide 5Boost.Hana
(Louis Dionne)
Template  metaprog. lib
A Brief History
# Slide 6A Brief History
Typefull
vs.
Monotype
Type based
vs.
Value based
meta objects
# Slide 7A Brief History
# Slide 8// Lib.hpp
class LibType : public BaseOne, public BaseTwo
{
   int a;
   double b;
};P2996
// main.cpp
#include <meta>
#include "Lib.hpp"
int main()
{
   constexpr std::meta::info refexpr = ^^LibType;
   constexpr auto res = std::meta::bases_of(refexpr)[0];
}https://godbolt.org/z/WxdvKdcMY
# Slide 9P2996
Reflection Operator: ^^
Splicers: [:…:]
std::meta::info
Metafunctions
8. define_aggregate (inject)
Name & Location:
3. Template Queries:
identifier_of
display_string_of
source_location_of
template_of
template_arguments_of
9. Data Layout:
4. Member Queries:
offset_of -> member_offset
size_of
alignment_of
bit_size_of
2. Type Queries:
members_of, bases_of
(non)static_data_members_of
enumerators_of
type_of
parent_of
dealias
	 
Access modifiers: is_public, is_protected, is_private
Inheritance: is_virtual, is_pure_virtual, is_override, ...
Encapsulation: is_class_member, is_namespace_member, is_explicit, is_deleted, ...
Advanced Type Queries: is_complete_type, is_template, is_special_member, ...
10. (+) Type Traits
(+ Other Type Predicates)
5. substitute (template)
6. extract<T>(info) (constexpr not required)
7. reflect_x (extended)
reflect_value
reflect_object
reflect_function
auto rexpr = ^ int;# Slide 10P2996
AKA Unibrow
^auto rexpr = ^^int;# Slide 11error: meta type variables must be constexpr
constexpr auto rexpr = ^^int;P2996
# Slide 12constexpr auto rexpr = ^^int;
typename[:rexpr:] a  = 42;What are you?
rexpr
meta::info obj, contains info on "int" type
typename[:rexpr:]
int
P2996
* More about the typename in P2996/[dcl.type.splice]
# Slide 13P2996
Reflection Operator: ^^
Splicers: [:…:]
std::meta::info
Metafunctions
8. define_aggregate (inject)
Name & Location:
3. Template Queries:
identifier_of
display_string_of
source_location_of
template_of
template_arguments_of
9. Data Layout:
4. Member Queries:
offset_of -> member_offset
size_of
alignment_of
bit_size_of
2. Type Queries:
members_of, bases_of
(non)static_data_members_of
enumerators_of
type_of
parent_of
dealias
	 
Access modifiers: is_public, is_protected, is_private
Inheritance: is_virtual, is_pure_virtual, is_override, ...
Encapsulation: is_class_member, is_namespace_member, is_explicit, is_deleted, ...
Advanced Type Queries: is_complete_type, is_template, is_special_member, ...
10. (+) Type Traits
(+ Other Type Predicates)
5. substitute (template)
6. extract<T>(info) (constexpr not required)
7. reflect_x (extended)
reflect_value
reflect_object
reflect_function
# Slide 14From: "Let's talk about abstraction layers"
P2996
# Slide 15int a = 42;
constexpr auto b = ^^a;
std::cout << [:b:] << "\n";       // OK
std::cout << [:^^(a*2):] << "\n"; // Errorconstexpr int a = 42;
constexpr auto b = ^^a;
std::cout << [:b:] << "\n";       // OK
std::cout << [:^^(a*2):] << "\n"; // OKP2996
Represents:
# Slide 16Reflection Operator: ^^
Splicers: [:…:]
std::meta::info
Metafunctions
P2996
8. define_aggregate (inject)
Name & Location:
3. Template Queries:
identifier_of
display_string_of
source_location_of
template_of
template_arguments_of
9. Data Layout:
4. Member Queries:
offset_of -> member_offset
size_of
alignment_of
bit_size_of
2. Type Queries:
members_of, bases_of
(non)static_data_members_of
enumerators_of
type_of
parent_of
dealias
	 
Access modifiers: is_public, is_protected, is_private
Inheritance: is_virtual, is_pure_virtual, is_override, ...
Encapsulation: is_class_member, is_namespace_member, is_explicit, is_deleted, ...
Advanced Type Queries: is_complete_type, is_template, is_special_member, ...
10. (+) Type Traits
(+ Other Type Predicates)
5. substitute (template)
6. extract<T>(info) (constexpr not required)
7. reflect_x (extended)
reflect_value
reflect_object
reflect_function
# Slide 17constexpr auto a = 42;
std::cout << meta::name_of(^^a) << "\n";All* the metafunctions accept "info" and return:
* Besides "reflect_value/object/function", which accepts type T
https://godbolt.org/z/9YW4bWGM7
constexpr auto a = 42;
std::cout << meta::identifier_of(^^a) << "\n";P2996
# Slide 18#include <iostream>
#include <experimental/meta>
class R;
constexpr std::meta::info res1 = ^^R;
constexpr auto print1 = std::meta::
				is_complete_type(res1); // (1)
class R {
    int a;
};
constexpr std::meta::info res2 = ^^R;
constexpr auto print2 = std::meta::
				is_complete_type(res2); // (2)
constexpr auto print3 = std::meta::
				is_complete_type(res1); // (3)
int main()
{
    std::cout << "Res: " << print1 << "\n"; // (1)
    std::cout << "Res: " << print2 << "\n"; // (2)
    std::cout << "Res: " << print3 << "\n"; // (3)
}https://godbolt.org/z/cafxMPo4o
Complexity of Reflection
# Slide 19Complexity of Reflection
# Slide 20int main()
{
    type.[:get_member_i(0):] = 1;
    type.[:get_member_i(1):] = 1;
    type.[:member_named("pub"):] = 2;
    type.[:member_named("prv"):] = 2;
}Library Code
User(*) Code
class LibType {
public:
    int pub;
private:
    int prv;
public:
    void Print();
};Complexity of Reflection
# Slide 21#include <string>
#include <experimental/meta>
consteval bool LogMembers(std::meta::info type) 
{
  // Verify type is a class/struct
  // Ignore usecase of unnamed members (union/struct etc.)
  std::__report_constexpr_value(identifier_of(type).data());
  for (auto r : nonstatic_data_members_of(type)) 
  {
      std::__report_constexpr_value("\n\tmember:");
      std::__report_constexpr_value(identifier_of(r).data());
  }
  return true;
}
// User code
struct Student 
{
  std::string name;
  int id;
};
static_assert(LogMembers(^^Student));Reflection-Based Library
Based on example from EDG's CE implementation
https://godbolt.org/z/Yddfxdq8j
# Slide 22Summary
Generate "MyTypeSerializer" class in a header (you can add .cpp file if needed), based on "MyType.h" which provide "serialize" and "deserialize" methods for "MyType", turning it into bytes buffer and back into a "MyType" class.
# Slide 23Summary
# Slide 24Summary
# Slide 25
Summary
(Disclaimer: as of now...)
No guarantees between executions
# Slide 26Rust's toolkit:
Summary
| C++ | Rust | |
|---|---|---|
| Approach | Functions / traits | Token manipulation | 
| Failure Outcome | Ill-formed / Compile | Compile | 
| Error indication | constexpr exception | Runtime / tests | 
# Slide 27Guarantees we do not provide:
The outcome of "breaking the contract":
Summary
# Slide 28Which "reflection libs" should go into the standard library?
Would love your input!
More info:
Summary
# Slide 29Inbal Levi
sinbal2lextra@gmail.com
linkedin.com/in/inballevi/
Thank you!
Stay in touch!
Thanks to:
Thank you for being passionate about C++!