Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pfr

> 编程语言
Open source

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

1.5K stars0 likes0 views
WebsiteGitHub

About

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

Boost.PFR

This C++14 library provides basic reflection capabilities, allowing access to structure elements by index, retrieval of aggregate field names, and std::tuple-like methods for user-defined types — all without macros or boilerplate code.

Boost.PFR is a part of the Boost 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 without boost:: namespace see PFR.

Test results

Branches Build Tests coverage More info Develop: details... Master: details...

Latest developer documentation

Motivating Example #0

#include <iostream>
#include <fstream>
#include <string>

#include "boost/pfr.hpp"

struct some_person {
  std::string name;
  unsigned birth_year;
};

int main(int argc, const char* argv[]) {
  some_person val{"Edgar Allan Poe", 1809};

  std::cout << boost::pfr::get<0>(val)                // No macros!
      << " was born in " << boost::pfr::get<1>(val);  // Works with any aggregate type!

  if (argc > 1) {
    std::ofstream ofs(argv[1]);
    ofs << boost::pfr::io(val);                       // File now contains: {"Edgar Allan Poe", 1809}
  }
}

Output:

Edgar Allan Poe was born in 1809

Run the above sample

Motivating Example #1

#include <iostream>
#include "boost/pfr.hpp"

struct my_struct { // no ostream operator defined!
    int i;
    char c;
    double d;
};

int main() {
    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"

struct my_struct { // no ostream operator defined!
    std::string s;
    int i;
};

int main() {
    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

…

Outputs:

(34 Chip Douglas 2500)

Requirements and Limitations

See docs.

License

Distributed under the Boost Software License, Version 1.0.

GitHub Issues· 45 open

View all on GitHub
  • #184

    Accessing addresses to members

    help wantedUpdated Jul 3, 2026
  • #185

    Is it possible to handle C arrays? Possible implementation linked

    help wantedUpdated Jul 2, 2026
  • #93

    Can packed structures be supported?

    Updated Jul 11, 2025
  • #80

    add support for bit-fields

    help wantedcompiler-or-standard-limitationUpdated Jun 20, 2025
  • #131

    PFR doesn't work with `std::vector<std::unique_ptr<T>>` field in C++20

    help wantedcompiler-or-standard-limitationUpdated Jun 20, 2025
  • #168

    tuple_size does not work on clang

    duplicatehelp wantedUpdated Jun 20, 2025
  • #204

    Add support for enums

    Updated Jun 2, 2025
  • #193

    Compile failed with field typed `std::optional<Eigen::Matrix3d>`

    duplicatecompiler-or-standard-limitationUpdated Dec 22, 2024
  • #186

    PFR does not work with non-copyable aggregates on MSVC 17.10

    compiler-or-standard-limitationUpdated Nov 8, 2024

Highlights

  • •C++
  • •boost
  • •clang
  • •cplusplus
  • •cplusplus-14

> Tags

C++boostclangcpluspluscplusplus-14

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言