optional icon indicating copy to clipboard operation
optional copied to clipboard

or_else of an Optional with a reference

Open juxeii opened this issue 4 years ago • 1 comments

All implementations of or_else in the case of an optional with a reference return optional<T>. I ran into an issue with that, and when changing all signatures to optional<T&> it was fixed. Isn't returning an optional<T> in these cases creating a copy of the contained reference?

Example:

struct IF
{
	virtual void foo() = 0;
};

struct Impl : IF
{
	void foo() override {}
};
Impl instance{};

tl::optional<IF&> test = tl::optional<IF&>{instance}.or_else([]() { std::cout << "bad"; });

juxeii avatar Apr 05 '20 11:04 juxeii

Can anyone confirm that the example above does not work? What am I missing?

juxeii avatar Apr 23 '20 05:04 juxeii