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

Allow implicit conversion to T to be able to use as optional<T>.value_or(...) parameter #1

Open
slnj opened this issue Apr 13, 2023 · 0 comments

Comments

@slnj
Copy link

slnj commented Apr 13, 2023

I'd like to be able to use Lazy as parameter for optional.value_or and make it be evaluated only if it is required.

Since the parameter must be implict convertible to T ( must verify is_convertible trait ), it should be good if
explicit operator reference() const;
is changed to
operator reference() const;

This will allow to do things like:

auto getFromFile = []() { std::cout << "File opened:   "; return std::make_tuple("Lazy get the value from some file!");};
auto lazy_get_from_file  = lazy::Lazy<std::string>( getFromFile );

std::optional<std::string> a= "Code value";
std::optional<std::string> b;

std::cout << "::Not empty optional::  "<< a.value_or( lazy_get_from_file ) << std::endl;
std::cout << "::Empty optional::  " << b.value_or( lazy_get_from_file ) << std::endl;

and get this as output:

::Not empty optional::  Code value
::Empty optional::  File opened:   Lazy get the value from some file!

Also bool converter should be removed and implemented as a member function like is_instantiated() in cast T is bool
Thanks

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

1 participant