Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

or_else of an Optional with a reference #40

Open
juxeii opened this issue Apr 5, 2020 · 2 comments
Open

or_else of an Optional with a reference #40

juxeii opened this issue Apr 5, 2020 · 2 comments

Comments

@juxeii
Copy link

juxeii commented Apr 5, 2020

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
Copy link
Author

juxeii commented Apr 23, 2020

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

@dedlocc
Copy link

dedlocc commented Nov 15, 2024

Hi, I ran into the same issue, with a use case similar to OP's. It seems that tl::optional<T&>::or_else should indeed return tl::optional<T&> instead of tl::optional<T>.

There are currently no test cases that would check any of those overloads for optional references.

I suggest changing the return types as described above. Let me know if you want me to create a PR for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants