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 maybe utility #4450

Open
randombit opened this issue Nov 27, 2024 · 0 comments
Open

Add maybe utility #4450

randombit opened this issue Nov 27, 2024 · 0 comments

Comments

@randombit
Copy link
Owner

randombit commented Nov 27, 2024

Perhaps its worth adding a utility for this pattern at one point. Something along those lines:

template <typename InnerT, typename T>
   requires std::constructible_from<InnerT, T> && requires(T t) {
      { static_cast<bool>(t) } -> std::same_as<bool>;
   }
auto maybe(T&& v) {
   std::optional<InnerT> result = std::nullopt;
   if(v) {
      result.emplace(std::forward<T>(v));
   }
   return result;
}

... the implementation of this function would then look like that:

   return maybe<EC_AffinePoint>(p._inner().group()->mul_px_qy(p._inner(), x._inner(), q._inner(), y._inner(), rng));

... and it would also be RVO-friendlier.

One downside: similarly to std::make_unique (and friends), this needs the constructor of InnerT (here: EC_AffinePoint(std::unique_ptr<EC_AffinePoint_Data>)) to be public. 😞 This would currently not be the case for this particular example.

Originally posted by @reneme in #4446 (comment)

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