Add helper class to store optional references. #414
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds
storm::OptionalRef
, a helper class that optionally holds a reference to an object.This mimics the interface of std::optional, except that an OptionalRef never takes ownership of an object.
A possible use case is the implementation of optional function arguments, where std::optional would trigger a deep copy of the object. For example,
foo(storm::OptionalRef<T const> bar = storm::NullRef)
instead offoo(T const& bar)
.Why?
This seems to be the best option for providing optional arguments:
foo(std::optional<A const> const& a) {...}
a lot, but it's imo very implicit that e.g. infoo(bar);
a copy ofbar
is created when it is of typeA
orA const&
.void foo(A const* a) {...}
which avoids a copy but it's not very explicit about the fact thata
is optional and it makes ownership unclear.std::optional<A const&>
is not allowed by the CPP standard.void foo(std::optional<std::reference_wrapper<A>> const& a) { ... }
but it becomes a bit annoying since in the body offoo
we'd have to deal with both, the optional and the reference_wrapper, e.g.A const& a_ref = a.value().get()
foo()
and afoo(A const& a)
is not always feasible (code duplications, dealing with multiple optional arguments, ...)