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

Add find_or_try_insert_with for fallible insert functions #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

krisvanrens
Copy link
Owner

@krisvanrens krisvanrens commented Oct 3, 2024

From the examples:

#include <cassert>
#include <memo_cache.hpp>

mc::memo_cache<int, std::string, 4> c;

assert(!c.contains(42));

auto v1 = c.find_or_try_insert_with(42, [] -> std::expected<std::string, std::string> { return "The Answer"; });

assert(v1 == "The Answer");
assert(c.find(42).has_value());
assert(c.find(42).value() == "The Answer");

auto v2 = c.find_or_try_insert_with(42, [] -> std::expected<std::string, std::string> { return std::unexpected{"Dunno"}; });

assert(!v2.has_value());
assert(v2.error() == "Dunno");

But this doesn't work yet because the compiler is not able to deduce fallible result type. See also: https://www.godbolt.org/z/o93qGsvzY

@krisvanrens krisvanrens added the enhancement New feature or request label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant