More work on implementing the optional_variant type

This commit is contained in:
Denis Blank 2018-03-08 14:24:09 +01:00
parent 224e8c835f
commit 42c04f0fcb

View File

@ -222,16 +222,19 @@ public:
template <typename V, std::size_t Index =
traits::index_of_t<std::decay_t<V>, T...>::value>
explicit optional_variant(V&& value) //
// Since the optional_variant is never a part of the contained
// values itself this overload is safed against the linted issue.
// NOLINTNEXTLINE(misc-forwarding-reference-overload)
explicit optional_variant(V&& value)
: optional_variant(std::forward<V>(value), Index) {
}
optional_variant& operator=(T value) {
set_value(std::move(value));
return *this;
}
optional_variant& operator=(types::error_type error) {
set_exception(std::move(error));
template <typename V, std::size_t Index =
traits::index_of_t<std::decay_t<V>, T...>::value>
optional_variant& operator=(V&& value) {
weak_destroy();
init(std::forward<V>(value));
set_slot(Index);
return *this;
}
@ -249,6 +252,18 @@ public:
return !is_empty();
}
template <typename V>
V& cast() noexcept {
assert(!is_slot(traits::index_of_t<std::decay_t<V>, T...>::value));
return *reinterpret_cast<std::decay_t<V>*>(&this->storage_);
}
template <typename V>
V const& cast() const noexcept {
assert(!is_slot(traits::index_of_t<std::decay_t<V>, T...>::value));
return *reinterpret_cast<std::decay_t<V> const*>(&this->storage_);
}
private:
template <typename V>
void visit(V&& visitor) {
@ -275,17 +290,6 @@ private:
}
}
template <typename V>
V& cast() noexcept {
assert(!is_empty());
return *reinterpret_cast<V*>(&this->storage_);
}
template <typename V>
V const& cast() const noexcept {
assert(!is_empty());
return *reinterpret_cast<V const*>(&this->storage_);
}
template <typename V>
void init(V&& value) {
assert(is_empty());