https://github.com/redskittlefox/reflexpr
Compile time struct Reflections
https://github.com/redskittlefox/reflexpr
aggregates cpp cpp20 cpp20-library cpp23 header-only reflections structs
Last synced: 8 months ago
JSON representation
Compile time struct Reflections
- Host: GitHub
- URL: https://github.com/redskittlefox/reflexpr
- Owner: RedSkittleFox
- License: mit
- Created: 2021-08-26T16:09:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-22T08:40:54.000Z (about 2 years ago)
- Last Synced: 2024-01-22T11:21:54.574Z (about 2 years ago)
- Topics: aggregates, cpp, cpp20, cpp20-library, cpp23, header-only, reflections, structs
- Language: C++
- Homepage:
- Size: 40 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/RedSkittleFox/reflexpr/actions/workflows/cmake-multi-platform.yml)
# reflexpr
is a c++20 compile and runtime aggregate reflections header only library. It allows you to iterate over aggregate type's member variables.
# Example Usage
```cpp
#include
#include
#include
struct my_aggregate
{
int a;
float b;
std::string c;
int& d;
};
int main()
{
int d = 5;
my_aggregate obj
{
.a = 1 ,
.b = 3.5f,
.c = "Foxes are great!",
.d = d
};
auto&& [v0, v1, v2, v3] = obj;
// Get Nth member - fox::reflexpr::get(aggregate)
std::cout << fox::reflexpr::get<0>(obj) << '\n'; // prints obj.a
// Iterate over members - fox::reflexpr::for_each(aggregate, func)
fox::reflexpr::for_each(obj, [](auto&& v) {std::cout << v << ' '; }), std::cout << '\n';
// Create a tuple-tie from members - fox::reflexpr::tie(aggregate)
auto tie = fox::reflexpr::tie(obj);
std::cout << (std::get<2>(tie) = 2) << '\n';
// Create a tuple from members - fox::reflexpr::make_tuple(aggregate)
auto tuple = fox::reflexpr::make_tuple(obj);
std::cout << (std::get<2>(tuple)) << '\n';
// Tuple size - fox::reflexpr::tuple_size_v
static_assert(fox::reflexpr::tuple_size_v == static_cast(4));
// Tuple Nth type
static_assert(std::is_same_v, int&>);
return 0;
}
```
# Limitation
Right now it supports only up to 40 member variables.