Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

std::tuple like methods for user defined types without any macro or boilerplate code

License

NotificationsYou must be signed in to change notification settings

boostorg/pfr

Repository files navigation

This is a C++14 library for very basic reflection that gives you access to structure elements by index and provides otherstd::tuple like methods for user defined types without any macro or boilerplate code.

Boost.PFR is a part of theBoost C++ Libraries. However, Boost.PFR is a header only library that does not depend on Boost. You can just copy the content of the "include" folder from the github into your project, and the library will work fine.

For a version of the library withoutboost:: namespace seePFR.

Test results

BranchesBuildTests coverageMore info
Develop:CIBuild statusCoverage Statusdetails...
Master:CIBuild statusCoverage Statusdetails...

Latest developer documentation

Motivating Example #0

#include<iostream>#include<fstream>#include<string>#include"boost/pfr.hpp"structsome_person {  std::string name;unsigned birth_year;};intmain(int argc,constchar* argv[]) {  some_person val{"Edgar Allan Poe",1809};  std::cout << boost::pfr::get<0>(val)// No macro!      <<" was born in" << boost::pfr::get<1>(val);// Works with any aggregate initializables!if (argc >1) {    std::ofstreamofs(argv[1]);    ofs <<boost::pfr::io(val);// File now contains: {"Edgar Allan Poe", 1809}  }}

Outputs:

Edgar Allan Poe was born in 1809

Run the above sample

Motivating Example #1

#include<iostream>#include"boost/pfr.hpp"structmy_struct {// no ostream operator defined!int i;char c;double d;};intmain() {    my_struct s{100,'H',3.141593};    std::cout <<"my_struct has" << boost::pfr::tuple_size<my_struct>::value        <<" fields:" <<boost::pfr::io(s) <<"\n";}

Outputs:

my_struct has 3 fields: {100, H, 3.14159}

Motivating Example #2

#include<iostream>#include"boost/pfr.hpp"structmy_struct {// no ostream operator defined!    std::string s;int i;};intmain() {    my_struct s{{"Das ist fantastisch!"},100};    std::cout <<"my_struct has" << boost::pfr::tuple_size<my_struct>::value        <<" fields:" <<boost::pfr::io(s) <<"\n";}

Outputs:

my_struct has 2 fields: {"Das ist fantastisch!", 100}

Motivating Example #3

#include<iostream>#include<string>#include<boost/config/warning_disable.hpp>#include<boost/spirit/home/x3.hpp>#include<boost/fusion/include/adapt_boost_pfr.hpp>#include"boost/pfr/io.hpp"namespacex3= boost::spirit::x3;structast_employee {// No BOOST_FUSION_ADAPT_STRUCT definedint age;    std::string forename;    std::string surname;double salary;};autoconst quoted_string = x3::lexeme['"' >> +(x3::ascii::char_ -'"') >>'"'];x3::rule<classemployee, ast_employee>const employee ="employee";autoconst employee_def =x3::lit("employee")    >> '{'    >>  x3::int_ >>','    >>  quoted_string >>','    >>  quoted_string >>','    >>  x3::double_    >>'}'    ;BOOST_SPIRIT_DEFINE(employee);int main() {    std::string str = R"(employee{34, "Chip", "Douglas", 2500.00})";    ast_employee emp;    x3::phrase_parse(str.begin(),                     str.end(),                     employee,                     x3::ascii::space,                     emp);    std::cout << boost::pfr::io(emp) << std::endl;}

Outputs:

(34 Chip Douglas 2500)

Requirements and Limitations

See docs.

License

Distributed under theBoost Software License, Version 1.0.


[8]ページ先頭

©2009-2025 Movatter.jp