TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24150 12
state!");
}
} else {
destroy();
_tag = other._tag;
switch (_tag) {
case Tag::Ok: new (&_value.ok) T(other._value.ok); break;
case Tag::Err: new (&_value.error) E(other._value.error); break;
default:
panic("[_tag] was left in an invalid state!");
}
}

return *this;
}

Result &operator=(Result &&other) noexcept {
if (this == &other) {
return *this;
}

if (_tag == other._tag) {
switch (_tag) {
case Tag::Ok: _value.ok = move(other._value.ok); break;
case Tag::Err: _value.error = move(other._value.error); break;
default:
panic("[_tag] was left in an invalid state!");
}
} else {
destroy();
_tag = other._tag;
switch (_tag) {
case Tag::Ok: new (&_value.ok) T(move(other._value.ok)); break;
case Tag::Err: new (&_value.error) E(move(other._value.error)); break;
default:
panic("[_tag] was left in an invalid state!");
}
}

return *this;
}

private:
enum class Tag {
Ok,
Err
} _tag;

union Variant {
T ok;
E error;

constexpr Variant() noexcept {}
~Variant() noexcept {}
} _value;

explicit Result(Tag tag, const T &ok, OkOverload)
: _tag{ tag } {
new (&_value.ok) T(ok);
}

explicit Result(Tag tag, const E &error, ErrOverload)
: _tag{ tag } {
new (&_value.error) E(error);
}

explicit Result(Tag tag, T &&ok, OkOverload)
: _tag{ tag } {
new (&_value.ok) T(move(ok));
}

explicit Result(Tag tag, E &&error, ErrOverload)
: _tag{ tag } {
new (&_value.error) E(move(error));
}

void destroy() noexcept {
switch (_tag) {
case Tag::Ok: _value.ok.~T(); break;
case Tag::Err: _value.error.~E(); break;
default:
panic("[_tag] was left in an invalid state!");
}
}
};

struct Unit {};

template<typename T>
using Option = Result<T, Unit>;

template<typename E>
using Fallible = Result<Unit, E>;

#endif

```

https://redd.it/1okpb9k
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 12, 2026[Release] slick-logger v2.2.0 — Lock-free asynchronous C++20 logging with multi-process sh…
  2. Oct 12, 2026Cheap alternative to reboot parameters (nohz, rcu, isolcpus) for stable benchmarks When no…
  3. Oct 12, 2026This C++ drama is crazy - Adobe votes no https://www.youtube.com/watch?v=DEqx_NMMH9Q https…
  4. Oct 12, 2026FastNoise2: noise generation using SIMD and C++17 https://github.com/Auburn/FastNoise2 htt…
  5. Oct 12, 2026New C++ Conference Videos Released This Month - October 2026 (Updated To Include Videos Re…
  6. Oct 12, 2026cxvm: pin a GCC or Clang version per project on Linux (pyenv/nvm, but for C++ compilers) I…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →