2
0
mirror of https://github.com/boostorg/pfr.git synced 2026-01-19 04:22:13 +00:00
Files
pfr/example/get_name.cpp
2023-09-02 22:42:54 +03:00

44 lines
1.3 KiB
C++

// Copyright (c) 2023 Bela Schaum, X-Ryl669, Denis Mikhailov.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Initial implementation by Bela Schaum, https://github.com/schaumb
// The way to make it union and UB free by X-Ryl669, https://github.com/X-Ryl669
//
#include <boost/pfr/config.hpp>
#if BOOST_PFR_ENABLE_GET_NAME_STATIC
//[pfr_example_get_name
/*`
Since C++20 it's possible to read name of structure fields by index using Boost.PFR library.
The following example shows how to do it using [funcref boost::pfr::get_name].
Let's define some structure:
*/
#include <boost/pfr/core_name.hpp>
struct foo { // defining structure
int some_integer;
char c;
};
/*`
We can access field's names of that structure by index:
*/
constexpr auto r1 = boost::pfr::get_name<0, foo>(); // reading name of field with index 0, returns string `some_integer`
constexpr auto r2 = boost::pfr::get_name<1, foo>(); // reading name of field with index 1, returns string `c`
//] [/pfr_example_get_name]
#endif
int main() {
#if BOOST_PFR_ENABLE_GET_NAME_STATIC
if (r1 != "some_integer") return 1;
if (r2 != "c") return 2;
#endif
return 0;
}