SlideShare a Scribd company logo
Reflection in C++Next
Anton Bikineev, 2017
Agenda
Reflection in C++Next
1. Reflection
2. Surviving proposals
• P0194R3: Static reflection
• P0590R0: A design for static reflection
3. Examples
Reflection or introspection?
Reflection in C++Next
Type introspection is the ability of a program to examine the
type or properties of an object at runtime.
Reflection is the ability of a computer program to examine,
introspect, and modify its own structure and behavior at
runtime.
(from Wikipedia)
Reflection or introspection?
Reflection in C++Next
Static type introspection is the ability of a program to examine
the type or properties of an object at compile-time.
Static reflection is the ability of a computer program to
examine, introspect, and modify its own structure and
behavior at compile-time.
Introspection? Type traits!
Reflection in C++Next
namespace std {
template <class T>
struct is_integral {
constexpr static bool value = /* */true;
};
template <class T> /* since c++14 */
constexpr bool is_integral_v = is_integral<T>::value;
template <class T>
struct add_pointer {
using type = T*;
};
template <class T>
using add_pointer_t = typename add_pointer<T>::type;
}
Introspection? Type traits!
Reflection in C++Next
template <class Archive, class T>
void load(const Archive& ar, T& t) {
if constexpr (std::is_integral_v<T>)
load_integral(ar, t);
if constexpr (std::is_floating_point_v<T>)
load_fp(ar, t);
else if constexpr (std::is_trivially_copyable_v<T>)
load_memset(ar, t);
else
load_unoptimized(ar, t);
}
Introspection? Type traits!
Reflection in C++Next
template <class T, class enable = void>
struct has_foo: std::false_type {};
template <class T>
struct has_foo<T,
std::void_t<decltype(std::declval<T>().foo())>>:
std::true_type {};
Introspection? Type traits!
Reflection in C++Next
• works only with types!
• no way to traverse members!
Reflection
Reflection in C++Next
Static reflection is the ability of a computer program to
examine, introspect, and modify its own structure and
behavior at compile-time.
Reflection
Reflection in C++Next
Static reflection is the ability of a computer program to
examine, introspect, and modify its own structure and
behavior at compile-time.
Metaprogramming definition
Reflection in C++Next
Metaprogramming is the writing of computer programs
that write or manipulate other programs (or themselves)
as their data, or that do part of the work at compile time
that would otherwise be done at run-time.
(from Wikipedia, some rev.)
C++ template metaprogramming
Reflection in C++Next
auto multiply_by_two(const std::array<int, 3>& array) {
auto result = array;
for (auto& i: result)
i *= 2;
return result;
}
std::array<int, 3> arr1{1, 2, 3};
auto arr2 = multiply_by_two(arr1);
C++ template metaprogramming
Reflection in C++Next
template <class T>
struct multiply_by_two;
template <class T, T... I>
struct multiply_by_two<std::integer_sequence<T, I...>> {
using type = std::integer_sequence<T, (I * 2)...>;
};
using seq1 = std::integer_sequence<int, 1, 2, 3>;
using seq2 = multiply_by_two<seq1>::type;
C++ constexpr metaprogramming
Reflection in C++Next
constexpr
auto multiply_by_two(const std::array<int, 3>& array) {
auto result = array;
for (auto& i: result)
i *= 2;
return result;
}
constexpr std::array<int, 3> arr1{1, 2, 3};
constexpr auto arr2 = multiply_by_two(arr1);
Reflection?
Reflection in C++Next
Reflection?
Reflection in C++Next
What we actually need…
Reflection in C++Next
struct foo {
int a;
double b;
string c;
};
struct soa_foo {
vector<int> as;
vector<double> bs;
vector<string> cs;
};
Reflection in C++Next
class Person
{
[[getter, setter]] std::string name;
[[getter, setter]] std::string email;
};
What we actually need…
Reflection in C++Next
class Person
{
[[getter, setter, serialized]] std::string name;
[[getter, setter, serialized]] std::string email;
};
What we actually need…
Reflection in C++Next
class [[polymorphically_serialized]] Person: IPerson
{
Person() { /* initializing code here */ }
[[getter, setter, serialized]] std::string name;
[[getter, setter, serialized]] std::string email;
};
What we actually need…
Study Group 7: Reflection
Reflection in C++Next
• N4428: Type property queries
- 4th revision
- date: 2015-04-08
• P0255R0: Reflection via template pack expansion
- 5th revision
- date: 2016-02-12
• P0194R3: Static reflection
- 7th revision
- date: 2017-02-06
• P0590R0: A design for static reflection
- 1st revision
- date: 2017-02-05
Study Group 7: Reflection
Reflection in C++Next
• N4428: Type property queries
- 4th revision
- date: 2015-04-08
• P0255R0: Reflection via template pack expansion
- 5th revision
- date: 2016-02-12
• P0194R3: Static reflection
- 7th revision
- date: 2017-02-06
• P0590R0: A design for static reflection
- 1st revision
- date: 2017-02-05
Static reflection (P0194R3)
Reflection in C++Next
by Matúš Chochlík,
Axel Naumann,
David Sankel
P0194R3: Static reflection
Reflection in C++Next
• core-language changes:
- operator $reflect(x);
- where x is an id-expression;
- returns a unique generated type for an entity x;
- this type satisfies one or more concepts defined
in the library;
- you operate on these types (metaobjects) by
calling meta- operations.
• library changes:
- std::reflect::Concept<m>
- std::reflect::Operation<m>
P0194R3: Static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
using person_m = $reflect(person);
static_assert(std::reflect::Variable<person_m>());
std::cout << std::reflect::get_base_name_v<person_m>;
P0194R3: Static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
using person_m = $reflect(person);
static_assert(std::reflect::Variable<person_m>());
std::cout << std::reflect::get_base_name_v<person_m>;
using type_m = std::reflect::get_type_t<person_m>;
static_assert(std::reflect::Type<type_m>());
P0194R3: Static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
using person_m = $reflect(person);
static_assert(std::reflect::Variable<person_m>());
std::cout << std::reflect::get_base_name_v<person_m>;
using type_m = std::reflect::get_type_t<person_m>;
static_assert(std::reflect::Type<type_m>());
using real_type =
std::reflect::get_reflected_type_t<type_m>;
static_assert(std::is_same_v<real_type, std::string>);
P0194R3: Static reflection.
Meta-object concepts
Reflection in C++Next
namespace std::experimental::reflect {
template <class T> concept bool Object();
template <class T> concept bool ObjectSequence();
template <Object T> concept bool Named();
template <Object T> concept bool Alias();
template <Object T> concept bool RecordMember();
template <Object T> concept bool Enumerator();
template <Object T> concept bool Variable();
template <Object T> concept bool ScopeMember();
template <Object T> concept bool Typed();
template <Object T> concept bool Namespace();
template <Object T> concept bool GlobalScope();
template <Object T> concept bool Class();
template <Object T> concept bool Enum();
template <Object T> concept bool Record();
template <Object T> concept bool Scope();
template <Object T> concept bool Type();
template <Object T> concept bool Constant();
template <Object T> concept bool Base();
}
P0194R3: Static reflection.
Class-like things
Reflection in C++Next
template <Object T> concept bool Record();
- a union or a class
template <Object T> concept bool Class();
- a class or a struct
template <Object T> concept bool Base();
- base classes
template <Object T> concept bool RecordMember();
- data member and member types
P0194R3: Static reflection.
Scopes
Reflection in C++Next
template <Object T> concept bool Namespace();
- a namespace
template <Object T> concept bool Scope();
- a namespace, class or enumeration scope
template <Object T> concept bool ScopeMember();
- something in a scope
template <Object T> concept bool GlobalScope();
- the global namespace, $reflect(::)
P0194R3: Static reflection.
Enums
Reflection in C++Next
template <Object T> concept bool Enum();
- an enum
template <Object T> concept bool Enumerator();
- an enumerator
P0194R3: Static reflection.
Types
Reflection in C++Next
template <Object T> concept bool Typed();
- something that has a type, e.g. member-variable
template <Object T> concept bool Type();
- a type
P0194R3: Static reflection.
Expressions
Reflection in C++Next
template <Object T> concept bool Variable();
- a variable
template <Object T> concept bool Constant();
- a constant-expression, like an enumerator
P0194R3: Static reflection.
Other
Reflection in C++Next
template <Object T> concept bool Named();
- something with a name;
template <Object T> concept bool Alias();
- an alias, like a typedef
template <Object T> concept bool ObjectSequence();
- object-sequence, type-list…
P0194R3:
Meta-object operations by
examples
Reflection in C++Next
P0194R3: Static reflection.
Enum-to-string
Reflection in C++Next
template <class Enum>
std::string to_string(Enum e) {
using namespace std::reflect;
using e_m = $reflect(Enum);
static_assert(std::reflect::Enum<e_m>());
std::string result;
for_each<get_enumerators_t<e_m>>([&](auto m) {
using en_m = decltype(m);
if (get_constant_v<en_m> == e)
result = get_base_name_v<en_m>;
});
return result;
}
P0194R3: Static reflection.
Generic equal
Reflection in C++Next
template <class T>
bool generic_equal(const T& a, const T& b) {
using namespace std::reflect;
using T_m = $reflect(T);
static_assert(std::reflect::Class<e_m>());
bool result = true;
for_each<get_data_members_t<T_m>>([&](auto m) {
using m_t = decltype(m);
result &= a.*get_pointer_v<m_t> ==
b.*get_pointer_v<m_t>;
});
return result;
}
P0194R3: Static reflection.
Generic equal with unreflect
Reflection in C++Next
template <class T>
bool generic_equal(const T& a, const T& b) {
using namespace std::reflect;
using T_m = $reflect(T);
static_assert(std::reflect::Class<e_m>());
bool result = true;
for_each<get_data_members_t<T_m>>([&](auto m) {
result &= a.$unreflect(m) ==
b.$unreflect(m);
});
return result;
}
P0194R3: Static reflection.
Templates parametrized by namespace
Reflection in C++Next
namespace foo {
void func1(const std::string&);
void func2(int);
int func3(int);
}
namespace bar {
void func1(const std::string&);
void func2(int);
int func3(int);
}
P0194R3: Static reflection.
Templates parametrized by namespace
Reflection in C++Next
template <class MN>
void algorithm(const string& str, int i) {
// [foo|bar]::func1(str)
$unreflect(MN)::func1(str);
// [foo|bar]::func2([foo|bar]::func3(i))
$unreflect(MN)::func2($unreflect(MN)::func3(i));
}
void func(const string& str, int i, bool want_foo) {
if (want_foo)
algorithm<$reflect(foo)>(str, i);
else
algorithm<$reflect(bar)>(str, i);
}
P0194R3: Static reflection.
String to identifier transformation
Reflection in C++Next
struct foo {
int bar;
};
constexpr const char* bar_name =
get_base_name_v<$reflect(foo::bar)>;
int get_???bar_name???(const foo&);
P0194R3: Static reflection.
String to identifier transformation
Reflection in C++Next
struct foo {
int bar;
};
constexpr const char* bar_name =
get_base_name_v<$reflect(foo::bar)>;
int $identifier(ct_concat(“get_”, bar_name))(const foo&);
P0194R3: Static reflection.
String to identifier transformation
Reflection in C++Next
struct foo {
int a;
double b;
string c;
};
struct soa_foo {
vector<int> as;
vector<double> bs;
vector<string> cs;
};
P0194R3: Static reflection
Reflection in C++Next
• supported:
- variables;
- data members;
- member types;
- enumerators;
- template instantiations;
- aliases
P0194R3: Static reflection
Reflection in C++Next
• not supported:
- declarations in namespaces (except for types);
- functions (yet);
- class templates (yet)
P0194R3: Static reflection.
Pros and cons
Reflection in C++Next
• Pros:
- reflection of different entities is supported;
- very lazy instantiation, only queried data is
instantiated;
- “you don’t pay for what you don’t use” at
compile-time;
- a good base for building higher-level and
domain-specific libraries;
- very extensible
P0194R3: Static reflection.
Pros and cons
Reflection in C++Next
• Cons:
- awkward and verbose usage;
- no attribute ideas
A design for static reflection
(P0590R0)
Reflection in C++Next
by Andrew Sutton,
Herb Sutter
P0590R0: A design for static reflection
Reflection in C++Next
• core-language changes:
- operator $x;
- where x is an id-expression;
- returns a unique object of a generated type for
an entity x;
- the object represents its meta information;
- you operate on these objects by calling
member-functions
• library changes:
- std::meta::reflection_type<X>
P0590R0: A design for static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
auto person_m = $person;
static_assert(std::meta::is_variable_v<
decltype(person_m)>);
std::cout << person_m.name();
P0590R0: A design for static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
auto person_m = $person;
static_assert(std::meta::is_variable_v<
decltype(person_m)>);
std::cout << person_m.name();
auto type_m = person_m.type();
static_assert(std::meta::is_type_v<decltype(type_m)>);
P0590R0: A design for static reflection.
Simple example
Reflection in C++Next
std::string person = "John";
auto person_m = $person;
static_assert(std::meta::is_variable_v<
decltype(person_m)>);
std::cout << person_m.name();
auto type_m = person_m.type();
static_assert(std::meta::is_type_v<decltype(type_m)>);
using real_type = ???;
P0194R3 vs P0590R0
Enum-to-string
Reflection in C++Next
template <class Enum>
std::string to_string(Enum e) {
using namespace std::reflect;
using e_m = $reflect(Enum);
static_assert(std::reflect::Enum<e_m>());
std::string result;
for_each<get_enumerators_t<e_m>>([&](auto m) {
using en_m = decltype(m);
if (get_constant_v<en_m> == e)
result = get_base_name_v<en_m>;
});
return result;
}
P0194R3 vs P0590R0
Enum-to-string
Reflection in C++Next
template <class Enum>
std::string to_string(Enum e) {
using namespace std::meta;
auto e_m = $Enum;
static_assert(std::meta::is_enum_v<decltype(e_m)>);
std::string result;
for_each(e_m.enumerators(), [&](auto m) {
if (m.value() == e)
result = m.name();
});
return result;
}
P0194R3 vs P0590R0
Enum-to-string
Reflection in C++Next
template <class Enum>
std::string to_string(Enum e) {
using namespace std::meta;
auto e_m = $Enum;
static_assert(std::meta::is_enum_v<decltype(e_m)>);
std::string result;
for (auto e: e_m.enumerators())
if (m.value() == e)
result = m.name();
return result;
}
P0194R3 vs P0590R0
Generic equal
Reflection in C++Next
template <class T>
bool generic_equal(const T& a, const T& b) {
bool result = true;
for (auto member: $T.member_variables()) {
auto ptr = member.pointer();
result &= a.*ptr == b.*ptr;
}
return result;
}
P0590R0: A design for static reflection.
Pros and cons
Reflection in C++Next
• Pros:
- reflection of different entities is supported;
- better syntax;
- a good base for building higher-level and
domain-specific libraries;
- extensible
P0590R0: A design for static reflection.
Pros and cons
Reflection in C++Next
• Cons:
- some extra data may still be instantiated by
compiler;
- no attribute ideas
Thank you for your attention!
Reflection in C++Next

More Related Content

PPTX
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Sergey Platonov
 
PDF
Clang tidy
Yury Yafimachau
 
PDF
Fuzzing: The New Unit Testing
Dmitry Vyukov
 
PDF
Антон Бикинеев, Writing good std::future&lt; C++ >
Sergey Platonov
 
PDF
Kirk Shoop, Reactive programming in C++
Sergey Platonov
 
PPTX
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Sergey Platonov
 
PDF
C++20 the small things - Timur Doumler
corehard_by
 
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
Platonov Sergey
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Sergey Platonov
 
Clang tidy
Yury Yafimachau
 
Fuzzing: The New Unit Testing
Dmitry Vyukov
 
Антон Бикинеев, Writing good std::future&lt; C++ >
Sergey Platonov
 
Kirk Shoop, Reactive programming in C++
Sergey Platonov
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Sergey Platonov
 
C++20 the small things - Timur Doumler
corehard_by
 
Евгений Крутько, Многопоточные вычисления, современный подход.
Platonov Sergey
 

What's hot (20)

PDF
TensorFlow XLA RPC
Mr. Vengineer
 
PDF
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Sergey Platonov
 
PPTX
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Sergey Platonov
 
PDF
C++ How I learned to stop worrying and love metaprogramming
cppfrug
 
PDF
Tiramisu をちょっと、味見してみました。
Mr. Vengineer
 
PDF
Tensor comprehensions
Mr. Vengineer
 
PDF
Joel Falcou, Boost.SIMD
Sergey Platonov
 
PPTX
Дмитрий Демчук. Кроссплатформенный краш-репорт
Sergey Platonov
 
PPTX
Алексей Кутумов, Вектор с нуля
Sergey Platonov
 
PDF
C++ references
corehard_by
 
PDF
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Mr. Vengineer
 
PDF
Tiramisu概要
Mr. Vengineer
 
PDF
TensorFlow local Python XLA client
Mr. Vengineer
 
PDF
Multithreading done right
Platonov Sergey
 
PPTX
Node.js System: The Landing
Haci Murat Yaman
 
PDF
Qt Rest Server
Vasiliy Sorokin
 
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
PDF
Алексей Кутумов, Coroutines everywhere
Sergey Platonov
 
PDF
C++の話(本当にあった怖い話)
Yuki Tamura
 
PPTX
The operation principles of PVS-Studio static code analyzer
Andrey Karpov
 
TensorFlow XLA RPC
Mr. Vengineer
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Sergey Platonov
 
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Sergey Platonov
 
C++ How I learned to stop worrying and love metaprogramming
cppfrug
 
Tiramisu をちょっと、味見してみました。
Mr. Vengineer
 
Tensor comprehensions
Mr. Vengineer
 
Joel Falcou, Boost.SIMD
Sergey Platonov
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Sergey Platonov
 
Алексей Кутумов, Вектор с нуля
Sergey Platonov
 
C++ references
corehard_by
 
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Mr. Vengineer
 
Tiramisu概要
Mr. Vengineer
 
TensorFlow local Python XLA client
Mr. Vengineer
 
Multithreading done right
Platonov Sergey
 
Node.js System: The Landing
Haci Murat Yaman
 
Qt Rest Server
Vasiliy Sorokin
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
Алексей Кутумов, Coroutines everywhere
Sergey Platonov
 
C++の話(本当にあった怖い話)
Yuki Tamura
 
The operation principles of PVS-Studio static code analyzer
Andrey Karpov
 
Ad

Viewers also liked (9)

PPTX
Алексей Кутумов, C++ без исключений, часть 3
Platonov Sergey
 
PPTX
Повседневный С++: алгоритмы и итераторы @ C++ Russia 2017
Mikhail Matrosov
 
PPTX
C++ Core Guidelines
Sergey Zubkov
 
PDF
Parallel STL
Evgeny Krutko
 
PPTX
Григорий Демченко, Универсальный адаптер
Sergey Platonov
 
PPTX
Сергей Шамбир, Адаптация Promise/A+ для взаимодействия между C++ и Javascript
Sergey Platonov
 
PPTX
Евгений Рыжков, Андрей Карпов Как потратить 10 лет на разработку анализатора ...
Platonov Sergey
 
PDF
Догнать и перегнать boost::lexical_cast
Roman Orlov
 
PPTX
Евгений Зуев, С++ в России: Стандарт языка и его реализация
Platonov Sergey
 
Алексей Кутумов, C++ без исключений, часть 3
Platonov Sergey
 
Повседневный С++: алгоритмы и итераторы @ C++ Russia 2017
Mikhail Matrosov
 
C++ Core Guidelines
Sergey Zubkov
 
Parallel STL
Evgeny Krutko
 
Григорий Демченко, Универсальный адаптер
Sergey Platonov
 
Сергей Шамбир, Адаптация Promise/A+ для взаимодействия между C++ и Javascript
Sergey Platonov
 
Евгений Рыжков, Андрей Карпов Как потратить 10 лет на разработку анализатора ...
Platonov Sergey
 
Догнать и перегнать boost::lexical_cast
Roman Orlov
 
Евгений Зуев, С++ в России: Стандарт языка и его реализация
Platonov Sergey
 
Ad

Similar to Антон Бикинеев, Reflection in C++Next (20)

PDF
C++ Training
SubhendraBasu5
 
PPTX
C++ metaprogramming
Arindam Mukherjee
 
PPTX
C++ metaprogramming
Arindam Mukherjee
 
PDF
C++ CoreHard Autumn 2018. Debug C++ Without Running - Anastasia Kazakova
corehard_by
 
PPTX
Object Oriented Code RE with HexraysCodeXplorer
Alex Matrosov
 
PDF
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
PPTX
C++ Templates. SFINAE
GlobalLogic Ukraine
 
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
PDF
Effective Object Oriented Design in Cpp
CodeOps Technologies LLP
 
PPTX
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
PDF
The Goal and The Journey - Turning back on one year of C++14 Migration
Joel Falcou
 
PDF
How to make a large C++-code base manageable
corehard_by
 
PDF
[E-Dev-Day-US-2015][8/9] he EFL API in Review (Tom Hacohen)
EnlightenmentProject
 
PDF
Modern C++
Michael Clark
 
PPTX
C++ Basics
Himanshu Sharma
 
PPTX
Useful C++ Features You Should be Using
Embarcadero Technologies
 
PDF
Eo fosdem 15
Samsung Open Source Group
 
PPTX
The Style of C++ 11
Sasha Goldshtein
 
PDF
Debugging and Profiling C++ Template Metaprograms
Platonov Sergey
 
PDF
c++-language-1208539706757125-9.pdf
nisarmca
 
C++ Training
SubhendraBasu5
 
C++ metaprogramming
Arindam Mukherjee
 
C++ metaprogramming
Arindam Mukherjee
 
C++ CoreHard Autumn 2018. Debug C++ Without Running - Anastasia Kazakova
corehard_by
 
Object Oriented Code RE with HexraysCodeXplorer
Alex Matrosov
 
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
C++ Templates. SFINAE
GlobalLogic Ukraine
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
Effective Object Oriented Design in Cpp
CodeOps Technologies LLP
 
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
The Goal and The Journey - Turning back on one year of C++14 Migration
Joel Falcou
 
How to make a large C++-code base manageable
corehard_by
 
[E-Dev-Day-US-2015][8/9] he EFL API in Review (Tom Hacohen)
EnlightenmentProject
 
Modern C++
Michael Clark
 
C++ Basics
Himanshu Sharma
 
Useful C++ Features You Should be Using
Embarcadero Technologies
 
The Style of C++ 11
Sasha Goldshtein
 
Debugging and Profiling C++ Template Metaprograms
Platonov Sergey
 
c++-language-1208539706757125-9.pdf
nisarmca
 

More from Sergey Platonov (20)

PDF
Полухин Антон, Как делать не надо: C++ велосипедостроение для профессионалов
Sergey Platonov
 
PDF
Василий Сорокин, Простой REST сервер на Qt с рефлексией
Sergey Platonov
 
PPTX
Лев Казаркин, Удивительные приключения регистров SSE или в поисках одного бага
Sergey Platonov
 
PDF
Павел Филонов, Разделяй и управляй вместе с Conan.io
Sergey Platonov
 
PPTX
Григорий Демченко, Асинхронность и неблокирующая синхронизация
Sergey Platonov
 
ODP
Антон Полухин. C++17
Sergey Platonov
 
PPTX
Павел Беликов, Как избежать ошибок, используя современный C++
Sergey Platonov
 
PDF
Денис Кандров, Пушкова Евгения, QSpec: тестирование графических приложений на Qt
Sergey Platonov
 
PPTX
Александр Тарасенко, Использование python для автоматизации отладки С/C++ код...
Sergey Platonov
 
PDF
Павел Довгалюк, Обратная отладка
Sergey Platonov
 
PPTX
Никита Глушков, К вопросу о реализации кроссплатформенных фреймворков
Sergey Platonov
 
PPTX
Dori Exterman, Considerations for choosing the parallel computing strategy th...
Sergey Platonov
 
PPTX
Александр Фокин, Рефлексия в C++
Sergey Platonov
 
PDF
Антон Нонко, Классические строки в C++
Sergey Platonov
 
PPTX
Михаил Матросов, Повседневный С++: boost и STL
Sergey Platonov
 
PDF
Борис Сазонов, RAII потоки и CancellationToken в C++
Sergey Platonov
 
PPTX
Илья Шишков, Принципы создания тестируемого кода
Sergey Platonov
 
PDF
Антон Наумович, Система автоматической крэш-аналитики своими средствами
Sergey Platonov
 
PDF
Андрей Карпов, Приватные байки от разработчиков анализатора кода
Sergey Platonov
 
PDF
Юрий Ефимочев, Компилируемые в реальном времени DSL для С++
Sergey Platonov
 
Полухин Антон, Как делать не надо: C++ велосипедостроение для профессионалов
Sergey Platonov
 
Василий Сорокин, Простой REST сервер на Qt с рефлексией
Sergey Platonov
 
Лев Казаркин, Удивительные приключения регистров SSE или в поисках одного бага
Sergey Platonov
 
Павел Филонов, Разделяй и управляй вместе с Conan.io
Sergey Platonov
 
Григорий Демченко, Асинхронность и неблокирующая синхронизация
Sergey Platonov
 
Антон Полухин. C++17
Sergey Platonov
 
Павел Беликов, Как избежать ошибок, используя современный C++
Sergey Platonov
 
Денис Кандров, Пушкова Евгения, QSpec: тестирование графических приложений на Qt
Sergey Platonov
 
Александр Тарасенко, Использование python для автоматизации отладки С/C++ код...
Sergey Platonov
 
Павел Довгалюк, Обратная отладка
Sergey Platonov
 
Никита Глушков, К вопросу о реализации кроссплатформенных фреймворков
Sergey Platonov
 
Dori Exterman, Considerations for choosing the parallel computing strategy th...
Sergey Platonov
 
Александр Фокин, Рефлексия в C++
Sergey Platonov
 
Антон Нонко, Классические строки в C++
Sergey Platonov
 
Михаил Матросов, Повседневный С++: boost и STL
Sergey Platonov
 
Борис Сазонов, RAII потоки и CancellationToken в C++
Sergey Platonov
 
Илья Шишков, Принципы создания тестируемого кода
Sergey Platonov
 
Антон Наумович, Система автоматической крэш-аналитики своими средствами
Sergey Platonov
 
Андрей Карпов, Приватные байки от разработчиков анализатора кода
Sergey Platonov
 
Юрий Ефимочев, Компилируемые в реальном времени DSL для С++
Sergey Platonov
 

Recently uploaded (20)

PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Presentation about variables and constant.pptx
safalsingh810
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Exploring AI Agents in Process Industries
amoreira6
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 

Антон Бикинеев, Reflection in C++Next

  • 2. Agenda Reflection in C++Next 1. Reflection 2. Surviving proposals • P0194R3: Static reflection • P0590R0: A design for static reflection 3. Examples
  • 3. Reflection or introspection? Reflection in C++Next Type introspection is the ability of a program to examine the type or properties of an object at runtime. Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime. (from Wikipedia)
  • 4. Reflection or introspection? Reflection in C++Next Static type introspection is the ability of a program to examine the type or properties of an object at compile-time. Static reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at compile-time.
  • 5. Introspection? Type traits! Reflection in C++Next namespace std { template <class T> struct is_integral { constexpr static bool value = /* */true; }; template <class T> /* since c++14 */ constexpr bool is_integral_v = is_integral<T>::value; template <class T> struct add_pointer { using type = T*; }; template <class T> using add_pointer_t = typename add_pointer<T>::type; }
  • 6. Introspection? Type traits! Reflection in C++Next template <class Archive, class T> void load(const Archive& ar, T& t) { if constexpr (std::is_integral_v<T>) load_integral(ar, t); if constexpr (std::is_floating_point_v<T>) load_fp(ar, t); else if constexpr (std::is_trivially_copyable_v<T>) load_memset(ar, t); else load_unoptimized(ar, t); }
  • 7. Introspection? Type traits! Reflection in C++Next template <class T, class enable = void> struct has_foo: std::false_type {}; template <class T> struct has_foo<T, std::void_t<decltype(std::declval<T>().foo())>>: std::true_type {};
  • 8. Introspection? Type traits! Reflection in C++Next • works only with types! • no way to traverse members!
  • 9. Reflection Reflection in C++Next Static reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at compile-time.
  • 10. Reflection Reflection in C++Next Static reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at compile-time.
  • 11. Metaprogramming definition Reflection in C++Next Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at run-time. (from Wikipedia, some rev.)
  • 12. C++ template metaprogramming Reflection in C++Next auto multiply_by_two(const std::array<int, 3>& array) { auto result = array; for (auto& i: result) i *= 2; return result; } std::array<int, 3> arr1{1, 2, 3}; auto arr2 = multiply_by_two(arr1);
  • 13. C++ template metaprogramming Reflection in C++Next template <class T> struct multiply_by_two; template <class T, T... I> struct multiply_by_two<std::integer_sequence<T, I...>> { using type = std::integer_sequence<T, (I * 2)...>; }; using seq1 = std::integer_sequence<int, 1, 2, 3>; using seq2 = multiply_by_two<seq1>::type;
  • 14. C++ constexpr metaprogramming Reflection in C++Next constexpr auto multiply_by_two(const std::array<int, 3>& array) { auto result = array; for (auto& i: result) i *= 2; return result; } constexpr std::array<int, 3> arr1{1, 2, 3}; constexpr auto arr2 = multiply_by_two(arr1);
  • 17. What we actually need… Reflection in C++Next struct foo { int a; double b; string c; }; struct soa_foo { vector<int> as; vector<double> bs; vector<string> cs; };
  • 18. Reflection in C++Next class Person { [[getter, setter]] std::string name; [[getter, setter]] std::string email; }; What we actually need…
  • 19. Reflection in C++Next class Person { [[getter, setter, serialized]] std::string name; [[getter, setter, serialized]] std::string email; }; What we actually need…
  • 20. Reflection in C++Next class [[polymorphically_serialized]] Person: IPerson { Person() { /* initializing code here */ } [[getter, setter, serialized]] std::string name; [[getter, setter, serialized]] std::string email; }; What we actually need…
  • 21. Study Group 7: Reflection Reflection in C++Next • N4428: Type property queries - 4th revision - date: 2015-04-08 • P0255R0: Reflection via template pack expansion - 5th revision - date: 2016-02-12 • P0194R3: Static reflection - 7th revision - date: 2017-02-06 • P0590R0: A design for static reflection - 1st revision - date: 2017-02-05
  • 22. Study Group 7: Reflection Reflection in C++Next • N4428: Type property queries - 4th revision - date: 2015-04-08 • P0255R0: Reflection via template pack expansion - 5th revision - date: 2016-02-12 • P0194R3: Static reflection - 7th revision - date: 2017-02-06 • P0590R0: A design for static reflection - 1st revision - date: 2017-02-05
  • 23. Static reflection (P0194R3) Reflection in C++Next by Matúš Chochlík, Axel Naumann, David Sankel
  • 24. P0194R3: Static reflection Reflection in C++Next • core-language changes: - operator $reflect(x); - where x is an id-expression; - returns a unique generated type for an entity x; - this type satisfies one or more concepts defined in the library; - you operate on these types (metaobjects) by calling meta- operations. • library changes: - std::reflect::Concept<m> - std::reflect::Operation<m>
  • 25. P0194R3: Static reflection. Simple example Reflection in C++Next std::string person = "John"; using person_m = $reflect(person); static_assert(std::reflect::Variable<person_m>()); std::cout << std::reflect::get_base_name_v<person_m>;
  • 26. P0194R3: Static reflection. Simple example Reflection in C++Next std::string person = "John"; using person_m = $reflect(person); static_assert(std::reflect::Variable<person_m>()); std::cout << std::reflect::get_base_name_v<person_m>; using type_m = std::reflect::get_type_t<person_m>; static_assert(std::reflect::Type<type_m>());
  • 27. P0194R3: Static reflection. Simple example Reflection in C++Next std::string person = "John"; using person_m = $reflect(person); static_assert(std::reflect::Variable<person_m>()); std::cout << std::reflect::get_base_name_v<person_m>; using type_m = std::reflect::get_type_t<person_m>; static_assert(std::reflect::Type<type_m>()); using real_type = std::reflect::get_reflected_type_t<type_m>; static_assert(std::is_same_v<real_type, std::string>);
  • 28. P0194R3: Static reflection. Meta-object concepts Reflection in C++Next namespace std::experimental::reflect { template <class T> concept bool Object(); template <class T> concept bool ObjectSequence(); template <Object T> concept bool Named(); template <Object T> concept bool Alias(); template <Object T> concept bool RecordMember(); template <Object T> concept bool Enumerator(); template <Object T> concept bool Variable(); template <Object T> concept bool ScopeMember(); template <Object T> concept bool Typed(); template <Object T> concept bool Namespace(); template <Object T> concept bool GlobalScope(); template <Object T> concept bool Class(); template <Object T> concept bool Enum(); template <Object T> concept bool Record(); template <Object T> concept bool Scope(); template <Object T> concept bool Type(); template <Object T> concept bool Constant(); template <Object T> concept bool Base(); }
  • 29. P0194R3: Static reflection. Class-like things Reflection in C++Next template <Object T> concept bool Record(); - a union or a class template <Object T> concept bool Class(); - a class or a struct template <Object T> concept bool Base(); - base classes template <Object T> concept bool RecordMember(); - data member and member types
  • 30. P0194R3: Static reflection. Scopes Reflection in C++Next template <Object T> concept bool Namespace(); - a namespace template <Object T> concept bool Scope(); - a namespace, class or enumeration scope template <Object T> concept bool ScopeMember(); - something in a scope template <Object T> concept bool GlobalScope(); - the global namespace, $reflect(::)
  • 31. P0194R3: Static reflection. Enums Reflection in C++Next template <Object T> concept bool Enum(); - an enum template <Object T> concept bool Enumerator(); - an enumerator
  • 32. P0194R3: Static reflection. Types Reflection in C++Next template <Object T> concept bool Typed(); - something that has a type, e.g. member-variable template <Object T> concept bool Type(); - a type
  • 33. P0194R3: Static reflection. Expressions Reflection in C++Next template <Object T> concept bool Variable(); - a variable template <Object T> concept bool Constant(); - a constant-expression, like an enumerator
  • 34. P0194R3: Static reflection. Other Reflection in C++Next template <Object T> concept bool Named(); - something with a name; template <Object T> concept bool Alias(); - an alias, like a typedef template <Object T> concept bool ObjectSequence(); - object-sequence, type-list…
  • 36. P0194R3: Static reflection. Enum-to-string Reflection in C++Next template <class Enum> std::string to_string(Enum e) { using namespace std::reflect; using e_m = $reflect(Enum); static_assert(std::reflect::Enum<e_m>()); std::string result; for_each<get_enumerators_t<e_m>>([&](auto m) { using en_m = decltype(m); if (get_constant_v<en_m> == e) result = get_base_name_v<en_m>; }); return result; }
  • 37. P0194R3: Static reflection. Generic equal Reflection in C++Next template <class T> bool generic_equal(const T& a, const T& b) { using namespace std::reflect; using T_m = $reflect(T); static_assert(std::reflect::Class<e_m>()); bool result = true; for_each<get_data_members_t<T_m>>([&](auto m) { using m_t = decltype(m); result &= a.*get_pointer_v<m_t> == b.*get_pointer_v<m_t>; }); return result; }
  • 38. P0194R3: Static reflection. Generic equal with unreflect Reflection in C++Next template <class T> bool generic_equal(const T& a, const T& b) { using namespace std::reflect; using T_m = $reflect(T); static_assert(std::reflect::Class<e_m>()); bool result = true; for_each<get_data_members_t<T_m>>([&](auto m) { result &= a.$unreflect(m) == b.$unreflect(m); }); return result; }
  • 39. P0194R3: Static reflection. Templates parametrized by namespace Reflection in C++Next namespace foo { void func1(const std::string&); void func2(int); int func3(int); } namespace bar { void func1(const std::string&); void func2(int); int func3(int); }
  • 40. P0194R3: Static reflection. Templates parametrized by namespace Reflection in C++Next template <class MN> void algorithm(const string& str, int i) { // [foo|bar]::func1(str) $unreflect(MN)::func1(str); // [foo|bar]::func2([foo|bar]::func3(i)) $unreflect(MN)::func2($unreflect(MN)::func3(i)); } void func(const string& str, int i, bool want_foo) { if (want_foo) algorithm<$reflect(foo)>(str, i); else algorithm<$reflect(bar)>(str, i); }
  • 41. P0194R3: Static reflection. String to identifier transformation Reflection in C++Next struct foo { int bar; }; constexpr const char* bar_name = get_base_name_v<$reflect(foo::bar)>; int get_???bar_name???(const foo&);
  • 42. P0194R3: Static reflection. String to identifier transformation Reflection in C++Next struct foo { int bar; }; constexpr const char* bar_name = get_base_name_v<$reflect(foo::bar)>; int $identifier(ct_concat(“get_”, bar_name))(const foo&);
  • 43. P0194R3: Static reflection. String to identifier transformation Reflection in C++Next struct foo { int a; double b; string c; }; struct soa_foo { vector<int> as; vector<double> bs; vector<string> cs; };
  • 44. P0194R3: Static reflection Reflection in C++Next • supported: - variables; - data members; - member types; - enumerators; - template instantiations; - aliases
  • 45. P0194R3: Static reflection Reflection in C++Next • not supported: - declarations in namespaces (except for types); - functions (yet); - class templates (yet)
  • 46. P0194R3: Static reflection. Pros and cons Reflection in C++Next • Pros: - reflection of different entities is supported; - very lazy instantiation, only queried data is instantiated; - “you don’t pay for what you don’t use” at compile-time; - a good base for building higher-level and domain-specific libraries; - very extensible
  • 47. P0194R3: Static reflection. Pros and cons Reflection in C++Next • Cons: - awkward and verbose usage; - no attribute ideas
  • 48. A design for static reflection (P0590R0) Reflection in C++Next by Andrew Sutton, Herb Sutter
  • 49. P0590R0: A design for static reflection Reflection in C++Next • core-language changes: - operator $x; - where x is an id-expression; - returns a unique object of a generated type for an entity x; - the object represents its meta information; - you operate on these objects by calling member-functions • library changes: - std::meta::reflection_type<X>
  • 50. P0590R0: A design for static reflection. Simple example Reflection in C++Next std::string person = "John"; auto person_m = $person; static_assert(std::meta::is_variable_v< decltype(person_m)>); std::cout << person_m.name();
  • 51. P0590R0: A design for static reflection. Simple example Reflection in C++Next std::string person = "John"; auto person_m = $person; static_assert(std::meta::is_variable_v< decltype(person_m)>); std::cout << person_m.name(); auto type_m = person_m.type(); static_assert(std::meta::is_type_v<decltype(type_m)>);
  • 52. P0590R0: A design for static reflection. Simple example Reflection in C++Next std::string person = "John"; auto person_m = $person; static_assert(std::meta::is_variable_v< decltype(person_m)>); std::cout << person_m.name(); auto type_m = person_m.type(); static_assert(std::meta::is_type_v<decltype(type_m)>); using real_type = ???;
  • 53. P0194R3 vs P0590R0 Enum-to-string Reflection in C++Next template <class Enum> std::string to_string(Enum e) { using namespace std::reflect; using e_m = $reflect(Enum); static_assert(std::reflect::Enum<e_m>()); std::string result; for_each<get_enumerators_t<e_m>>([&](auto m) { using en_m = decltype(m); if (get_constant_v<en_m> == e) result = get_base_name_v<en_m>; }); return result; }
  • 54. P0194R3 vs P0590R0 Enum-to-string Reflection in C++Next template <class Enum> std::string to_string(Enum e) { using namespace std::meta; auto e_m = $Enum; static_assert(std::meta::is_enum_v<decltype(e_m)>); std::string result; for_each(e_m.enumerators(), [&](auto m) { if (m.value() == e) result = m.name(); }); return result; }
  • 55. P0194R3 vs P0590R0 Enum-to-string Reflection in C++Next template <class Enum> std::string to_string(Enum e) { using namespace std::meta; auto e_m = $Enum; static_assert(std::meta::is_enum_v<decltype(e_m)>); std::string result; for (auto e: e_m.enumerators()) if (m.value() == e) result = m.name(); return result; }
  • 56. P0194R3 vs P0590R0 Generic equal Reflection in C++Next template <class T> bool generic_equal(const T& a, const T& b) { bool result = true; for (auto member: $T.member_variables()) { auto ptr = member.pointer(); result &= a.*ptr == b.*ptr; } return result; }
  • 57. P0590R0: A design for static reflection. Pros and cons Reflection in C++Next • Pros: - reflection of different entities is supported; - better syntax; - a good base for building higher-level and domain-specific libraries; - extensible
  • 58. P0590R0: A design for static reflection. Pros and cons Reflection in C++Next • Cons: - some extra data may still be instantiated by compiler; - no attribute ideas
  • 59. Thank you for your attention! Reflection in C++Next