File: as.md

package info (click to toggle)
reflect-cpp 0.21.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,128 kB
  • sloc: cpp: 50,336; python: 139; makefile: 30; sh: 3
file content (81 lines) | stat: -rw-r--r-- 1,668 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# `rfl::as`

`rfl::as` allows you to cast structs as other structs. This is useful if the fields in `B` are a subset of the fields in `A`
or we need the fields in `A` and `B` to produce `C`.

This only works for the `rfl::Field`-syntax.

For example:

```cpp
struct A {
    std::string f1;
    std::string f2;
};

struct B {
    std::string f3;
    std::string f4;
};

struct C {
    std::string f1;
    std::string f2;
    std::string f4;
};

const auto a = A{.f1 = "Hello", .f2 = "World"};

const auto b = B{.f3 = "Hello", .f4 = "World"};

// Creates c by retrieving f1 and f2 from a and f4 from b. 
// Ignores f3.
const auto c = rfl::as<C>(a, b);
```

In this case, `f1`, `f2` and `f4` are now copied into the newly created `c` of type `C`.

Much like `rfl::replace`, `rfl::as` supports move semantics:

```cpp
auto a = A{.f1 = "Hello", .f2 = "World"};

auto b = B{.f3 = "Hello", .f4 = "World"};

const auto c = rfl::as<C>(std::move(a), std::move(b));
```

In this case, `f1`, `f2` and `f4` are now moved into the newly created `c` of type `C`. This is a zero-copy operation. `f3` is discarded.

`rfl::as` also supports `rfl::Flatten`. For instance:

```cpp
struct A {
    std::string f1;
    std::string f2;
};

struct B {
    std::string f3;
    std::string f4;
};

struct C {
    rfl::Flatten<A> a;
    rfl::Flatten<B> b;
    int f5;
};

const auto a = A{.f1 = "Hello", .f2 = "World"};

const auto b = B{.f3 = "Hello", .f4 = "World"};

const auto c =
    rfl::as<C>(a, b, rfl::make_field<"f5">(5));
```

In this particular instance, `rfl::as<C>(...)` is basically equivalent to this:

```cpp
const auto c = C{.a = a, .b = b, .f5 = rfl::make_field<"f5">(5)};
```