boostorg / boostorg/pfr

want to add `pointers to data members -> index`

Offen
#60 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
1.5k
Forks
176
Ø Merge
5 T. 21 Std.
Gemergte PRs (30 T.)
1

Beschreibung

```C++
template
struct DelayConstruct {
static inline T value{}; // construct in runtime.
};
template
constexpr std::size_t get_index(M T::*mem_ptr) {
auto &t = DelayConstruct::value;
return
std::apply([&](auto&...e) {
int idx = 0;
std::array list = { (void*)&e ... };
for (auto b : list) {
if (b == &(t.*mem_ptr))
return idx;
idx += 1;
}
return 42;
}, structure_tie(t));
}
```

```C++
//example1
struct S {
std::string x;
std::vector y;
std::string z;
};

static_assert(boost::pfr::get_index(&S::x) == 0);
static_assert(boost::pfr::get_index(&S::y) == 1);
static_assert(boost::pfr::get_index(&S::z) == 2);
```

```C++
//example2
struct S {
static constexpr auto names = std::make_tuple("x","y","z");
std::string x;
std::vector y;
std::string z;
};

assert(std::string_view{"x"} == std::get(S::names));
assert(std::string_view{"y"} == std::get(S::names));
assert(std::string_view{"z"} == std::get(S::names));
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.