TartanLlama / optional

C++11/14/17 std::optional with functional-style extensions and reference support
https://tl.tartanllama.xyz
Creative Commons Zero v1.0 Universal
859 stars 67 forks source link

or_else of an Optional with a reference #40

Open juxeii opened 4 years ago

juxeii commented 4 years ago

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 commented 4 years ago

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