2
0
mirror of https://github.com/boostorg/pfr.git synced 2026-01-19 04:22:13 +00:00

Improved docs

This commit is contained in:
Antony Polukhin
2016-04-08 21:56:51 +03:00
parent d6bf6d1e74
commit 7e080019ce

View File

@@ -164,87 +164,6 @@ namespace pod_ops;
* Enums will be returned as their underlying type
* Static variables are ignored
### Cool usecase examples
* Auto generate comparisons:
```c++
namespace pod_ops { // Contains comparisons suitable for any POD type
template <class T1, class T2>
inline bool operator==(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) == flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator!=(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) != flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator<(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) < flat_make_tuple(rhs);
}
template <class T1, class T2>
inline bool operator>(const T1& lhs, const T2& rhs) {
return flat_make_tuple(lhs) > flat_make_tuple(rhs);
}
} // pod_ops
// ...
struct comparable_struct {
int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
}; // No `operator ==` or `operator<` defined for the structure
using namespace pod_ops;
comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
comparable_struct s2 {0, 1, "Hello", false, 6,7,8,9,10,11111};
assert(s1 == s1); // uses pod_ops::operator==
assert(s1 < sq); // uses pod_ops::operator<
```
* Universal ostream operator<<:
```c++
template <std::size_t I, std::size_t N>
struct print_impl {
template <class T>
static void print (std::ostream& out, const T& value) {
if (!!I) out << ", ";
out << flat_get<I>(value);
print_impl<I + 1, N>::print(out, value);
}
};
template <std::size_t I>
struct print_impl<I, I> {
template <class T> static void print (std::ostream&, const T&) {}
};
// Universal `operator <<` for all POD types
template <class Ostreamable, class T>
typename std::enable_if<std::is_pod<T>::value && std::is_class<T>::value, Ostreamable& >::type
operator<<(Ostreamable& out, const T& value)
{
out << boost::typeindex::type_id<T>().pretty_name() << " { ";
print_impl<0, flat_tuple_size_v<T> >::print(out, value);
out << " };";
return out;
}
// ...
// No `operator<<` defined for that structure:
comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
// Using universal operator<<
std::cout << s1 << std::endl; // Outputs: { 0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11 }
```
### License
Distributed under the [Boost Software License, Version 1.0](http://boost.org/LICENSE_1_0.txt).