Skip to content

Commit cddd314

Browse files
committed
[expected] add getters for void value and error
1 parent 40f4d79 commit cddd314

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

include/itlib/expected.hpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// itlib-expected v1.02
1+
// itlib-expected v1.03
22
//
33
// A union-type of a value and an error
44
//
55
// SPDX-License-Identifier: MIT
66
// MIT License:
7-
// Copyright(c) 2021-2022 Borislav Stanimirov
7+
// Copyright(c) 2021-2025 Borislav Stanimirov
88
//
99
// Permission is hereby granted, free of charge, to any person obtaining
1010
// a copy of this software and associated documentation files(the
@@ -28,6 +28,7 @@
2828
//
2929
// VERSION HISTORY
3030
//
31+
// 1.03 (2025-01-23) Add value and error "getters" in void specializations
3132
// 1.02 (2022-09-02) Specializations for ref and void values and void errors
3233
// 1.01 (2021-09-27) Fixed value_or which could return a ref to temporary
3334
// 1.00 (2021-09-26) Initial release
@@ -466,7 +467,11 @@ class expected<void, E> {
466467
bool has_error() const { return !m_has_value; }
467468
explicit operator bool() const { return m_has_value; }
468469

469-
// value getters: none
470+
// value "getter"
471+
472+
void value() const noexcept {
473+
assert(has_value());
474+
}
470475

471476
// error getters
472477

@@ -621,7 +626,11 @@ class expected<T, void>
621626
T* operator->() { return &value(); }
622627
const T* operator->() const { return &value(); }
623628

624-
// error getters: none
629+
// error "getter"
630+
631+
void error() const noexcept {
632+
assert(has_error());
633+
}
625634

626635
private:
627636
union
@@ -664,7 +673,11 @@ class expected<T&, void> {
664673
T& value_or(T& v) const { return has_value() ? value() : v; }
665674
T* operator->() const { return &value(); }
666675

667-
// error getters: none
676+
// error "getter"
677+
678+
void error() const noexcept {
679+
assert(has_error());
680+
}
668681

669682
private:
670683
value_type* m_value;
@@ -693,9 +706,17 @@ class expected<void, void> {
693706
void clear() { m_has_value = false; }
694707
void emplace() { m_has_value = true; }
695708

696-
// value getters: none
709+
// value "getter"
710+
711+
void value() const noexcept {
712+
assert(has_value());
713+
}
697714

698-
// error getters: none
715+
// error "getter"
716+
717+
void error() const noexcept {
718+
assert(has_error());
719+
}
699720

700721
private:
701722
bool m_has_value;

test/t-expected-11.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ TEST_CASE("void basic")
409409
itlib::expected<void, int> y;
410410
CHECK(y);
411411
CHECK(y.has_value());
412+
y.value(); // should compile and safely do nothing
412413

413414
auto v = vfunc(true);
414415
CHECK(v.has_value());
@@ -527,6 +528,7 @@ TEST_CASE("eoptional")
527528
CHECK_FALSE(io);
528529
CHECK(io.has_error());
529530
CHECK(io.value_or(44) == 44);
531+
io.error(); // should compile and safely do nothing
530532

531533
io.emplace(5);
532534
CHECK(io);
@@ -574,10 +576,12 @@ TEST_CASE("eoptional void")
574576
CHECK(vo);
575577
CHECK(vo.has_value());
576578
CHECK(!vo.has_error());
579+
vo.value(); // should compile and safely do nothing
577580

578581
vo.clear();
579582
CHECK_FALSE(vo);
580583
CHECK(vo.has_error());
584+
vo.error(); // should compile and safely do nothing
581585

582586
itlib::eoptional<void> vo2 = itlib::unexpected();
583587
CHECK_FALSE(vo2);

0 commit comments

Comments
 (0)