pfr
2.2.0
이것은 인덱스별로 구조 요소에 액세스 할 수있는 매우 기본적인 반사를위한 C ++ 14 라이브러리로, 매크로 또는 보일러 플레이트 코드가없는 사용자 정의 유형에 대한 다른 std::tuple 메소드를 제공합니다.
boost.pfr은 부스트 C ++ 라이브러리의 일부입니다. 그러나 boost.pfr은 부스트에 의존하지 않는 헤더 전용 라이브러리입니다. GitHub의 "포함"폴더의 내용을 프로젝트에 복사하면 라이브러리가 제대로 작동합니다.
boost:: 네임 스페이스를 참조하십시오.
| 가지 | 짓다 | 적용 범위를 테스트합니다 | 더 많은 정보 |
|---|---|---|---|
| 개발하다: | ![]() | 세부... | |
| 주인: | ![]() | 세부... |
최신 개발자 문서
# 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
위의 샘플을 실행하십시오
# 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}
# 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}
# 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)
문서를 참조하십시오.
Boost 소프트웨어 라이센스, 버전 1.0에 따라 배포됩니다.