Boost
arrow_drop_down
Boost.Pfr
M
D

本次发布

Antony Polukhin
Antony Polukhin
作者
sdarwin
sdarwin
贡献者 - 新
Peter Dimov
Peter Dimov
贡献者

依赖项

库依赖关系将很快生成,请稍后再查看。

Boost.PFR

这是一个 C++14 库,用于非常基础的反射,它允许您通过索引访问结构体元素,并为用户定义的类型提供其他类似 std::tuple 的方法,而无需任何宏或样板代码。

Boost.PFRBoost C++ Libraries 的一部分。但是,Boost.PFR 是一个纯头文件库,不依赖于 Boost。您可以直接将 GitHub 中“include”文件夹的内容复制到您的项目中,该库就可以正常工作。

有关不带 boost:: 命名空间版本的库,请参阅 PFR

测试结果

分支 构建 测试覆盖率 更多信息
开发分支 CI Build status Coverage Status 详情...
主分支 CI Build status Coverage Status 详情...

最新的开发者文档

激励示例 #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 macro!
      << " was born in " << boost::pfr::get<1>(val);  // Works with any aggregate initializables!

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

输出

Edgar Allan Poe was born in 1809

运行上述示例

激励示例 #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";
}

输出

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

激励示例 #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";
}

输出

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

激励示例 #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"

namespace x3 = boost::spirit::x3;

struct ast_employee { // No BOOST_FUSION_ADAPT_STRUCT defined
    int age;
    std::string forename;
    std::string surname;
    double salary;
};

auto const quoted_string = x3::lexeme['"' >> +(x3::ascii::char_ - '"') >> '"'];

x3::rule<class employee, ast_employee> const employee = "employee";
auto const 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;
}

输出

(34 Chip Douglas 2500)

要求和限制

请参阅文档.

License

根据 Boost Software License, Version 1.0 分发。

全部时间

Denis Mikhailov
Denis Mikhailov
贡献者
bela
bela
贡献者
Alexander Malkov
Alexander Malkov
贡献者
Anarthal (Rubén Pérez)
Anarthal (Rubén Pérez)
贡献者
Rene Rivera
Rene Rivera
贡献者
Jean-Michaël Celerier
Jean-Michaël Celerier
贡献者
caozhanhao
caozhanhao
贡献者
Markus F.X.J. Oberhumer
Markus F.X.J. Oberhumer
贡献者
Alexander Karatarakis
Alexander Karatarakis
贡献者
Lena
Lena
贡献者
Zachary Wassall
贡献者
Edward Diener
Edward Diener
贡献者
Mohammad Nejati
Mohammad Nejati
贡献者
jatin-25
jatin-25
贡献者
Aleksei Romanov
Aleksei Romanov
贡献者