@@ -1532,6 +1532,16 @@ template<class T, class E, class F, class R = ...> R operator|( result<T, E>&& r
1532
1532
template<class E, class F, class R = ...> R operator|( result<void, E> const& r, F&& f );
1533
1533
template<class E, class F, class R = ...> R operator|( result<void, E>&& r, F&& f );
1534
1534
1535
+ // operator&
1536
+
1537
+ template<class T, class E, class F, class U = ...>
1538
+ result<U, E> operator&( result<T, E> const& r, F&& f );
1539
+ template<class T, class E, class F, class U = ...>
1540
+ result<U, E> operator&( result<T, E>&& r, F&& f );
1541
+
1542
+ template<class T, class E, class F, class R = ...> R operator&( result<T, E> const& r, F&& f );
1543
+ template<class T, class E, class F, class R = ...> R operator&( result<T, E>&& r, F&& f );
1544
+
1535
1545
} // namespace system
1536
1546
} // namespace boost
1537
1547
```
@@ -2607,6 +2617,90 @@ int get_port()
2607
2617
}
2608
2618
```
2609
2619
2620
+ #### operator&
2621
+
2622
+ ```
2623
+ template<class T, class E, class F, class U = ...>
2624
+ result<U, E> operator&( result<T, E> const& r, F&& f );
2625
+
2626
+ template<class T, class E, class F, class U = ...>
2627
+ result<U, E> operator&( result<T, E>&& r, F&& f );
2628
+ ```
2629
+ [none]
2630
+ * {blank}
2631
+ +
2632
+ Returns the error in `r`, or if `r` contains a value, transforms the value by calling `f` on it.
2633
+ +
2634
+ Let `U` be the type of `f(*r)`.
2635
+ +
2636
+ Effects: ::
2637
+ - If `r.has_value()` is `true`, returns `f(*r)`.
2638
+ - Otherwise, returns `r.error()`.
2639
+ Remarks: ::
2640
+ Only enabled when `U` is not an instance of `result`.
2641
+ Example: ::
2642
+ +
2643
+ ```
2644
+ struct currency_type
2645
+ {
2646
+ char code_[ 4 ] = {};
2647
+ };
2648
+
2649
+ result<double> get_exchange_rate( currency_type from, currency_type to );
2650
+
2651
+ result<double> convert( double amount, currency_type from, currency_type to )
2652
+ {
2653
+ return get_exchange_rate( from, to ) & [&](double rate){ return rate * amount; };
2654
+ }
2655
+ ```
2656
+
2657
+ ```
2658
+ template<class T, class E, class F, class R = ...> R operator&( result<T, E> const& r, F&& f );
2659
+ template<class T, class E, class F, class R = ...> R operator&( result<T, E>&& r, F&& f );
2660
+ ```
2661
+ [none]
2662
+ * {blank}
2663
+ +
2664
+ Returns the error in `r`, or if `r` contains a value, another `result` obtained
2665
+ by invoking the function `f` on the value in `r`.
2666
+ +
2667
+ Let `R` be the type of `f(*r)`.
2668
+ +
2669
+ Effects: ::
2670
+ - If `r.has_value()` is `true`, returns `f(*r)`.
2671
+ - Otherwise, returns `r.error()`.
2672
+ Remarks: ::
2673
+ Only enabled when `R` is an instance of `result` and `E` is convertible to `R::error_type`.
2674
+ Example: ::
2675
+ +
2676
+ ```
2677
+ struct JsonValue
2678
+ {
2679
+ result<JsonValue const&> at( std::size_t i ) const noexcept;
2680
+ result<JsonValue const&> at( std::string_view key ) const noexcept;
2681
+ template<class T> result<T> to_number() const noexcept;
2682
+ };
2683
+
2684
+ namespace helpers
2685
+ {
2686
+ inline auto at( std::size_t i ) {
2687
+ return [=](JsonValue const& jv){ return jv.at( i ); }; }
2688
+
2689
+ inline auto at( std::string_view key ) {
2690
+ return [=](JsonValue const& jv){ return jv.at( key ); }; }
2691
+
2692
+ template<class T> inline auto to_number() {
2693
+ return [](JsonValue const& jv){ return jv.to_number<T>(); }; }
2694
+
2695
+ } // namespace helpers
2696
+
2697
+ int get_port( JsonValue const& config, int def )
2698
+ {
2699
+ using namespace helpers;
2700
+ return config.at( "servers" ) & at( 0 ) & at( "port" ) & to_number<int>() | def;
2701
+ }
2702
+ ```
2703
+
2610
2704
## <boost/system.hpp>
2611
2705
2612
2706
This convenience header includes all the headers previously described.
0 commit comments