|
1 |
| -// itlib-expected v1.02 |
| 1 | +// itlib-expected v1.03 |
2 | 2 | //
|
3 | 3 | // A union-type of a value and an error
|
4 | 4 | //
|
5 | 5 | // SPDX-License-Identifier: MIT
|
6 | 6 | // MIT License:
|
7 |
| -// Copyright(c) 2021-2022 Borislav Stanimirov |
| 7 | +// Copyright(c) 2021-2025 Borislav Stanimirov |
8 | 8 | //
|
9 | 9 | // Permission is hereby granted, free of charge, to any person obtaining
|
10 | 10 | // a copy of this software and associated documentation files(the
|
|
28 | 28 | //
|
29 | 29 | // VERSION HISTORY
|
30 | 30 | //
|
| 31 | +// 1.03 (2025-01-23) Add value and error "getters" in void specializations |
31 | 32 | // 1.02 (2022-09-02) Specializations for ref and void values and void errors
|
32 | 33 | // 1.01 (2021-09-27) Fixed value_or which could return a ref to temporary
|
33 | 34 | // 1.00 (2021-09-26) Initial release
|
@@ -466,7 +467,11 @@ class expected<void, E> {
|
466 | 467 | bool has_error() const { return !m_has_value; }
|
467 | 468 | explicit operator bool() const { return m_has_value; }
|
468 | 469 |
|
469 |
| - // value getters: none |
| 470 | + // value "getter" |
| 471 | + |
| 472 | + void value() const noexcept { |
| 473 | + assert(has_value()); |
| 474 | + } |
470 | 475 |
|
471 | 476 | // error getters
|
472 | 477 |
|
@@ -621,7 +626,11 @@ class expected<T, void>
|
621 | 626 | T* operator->() { return &value(); }
|
622 | 627 | const T* operator->() const { return &value(); }
|
623 | 628 |
|
624 |
| - // error getters: none |
| 629 | + // error "getter" |
| 630 | + |
| 631 | + void error() const noexcept { |
| 632 | + assert(has_error()); |
| 633 | + } |
625 | 634 |
|
626 | 635 | private:
|
627 | 636 | union
|
@@ -664,7 +673,11 @@ class expected<T&, void> {
|
664 | 673 | T& value_or(T& v) const { return has_value() ? value() : v; }
|
665 | 674 | T* operator->() const { return &value(); }
|
666 | 675 |
|
667 |
| - // error getters: none |
| 676 | + // error "getter" |
| 677 | + |
| 678 | + void error() const noexcept { |
| 679 | + assert(has_error()); |
| 680 | + } |
668 | 681 |
|
669 | 682 | private:
|
670 | 683 | value_type* m_value;
|
@@ -693,9 +706,17 @@ class expected<void, void> {
|
693 | 706 | void clear() { m_has_value = false; }
|
694 | 707 | void emplace() { m_has_value = true; }
|
695 | 708 |
|
696 |
| - // value getters: none |
| 709 | + // value "getter" |
| 710 | + |
| 711 | + void value() const noexcept { |
| 712 | + assert(has_value()); |
| 713 | + } |
697 | 714 |
|
698 |
| - // error getters: none |
| 715 | + // error "getter" |
| 716 | + |
| 717 | + void error() const noexcept { |
| 718 | + assert(has_error()); |
| 719 | + } |
699 | 720 |
|
700 | 721 | private:
|
701 | 722 | bool m_has_value;
|
|
0 commit comments