C++ Logo


Advanced search

Re: [std-proposals] find_integral_constant

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Wed, 28 Sep 2022 15:26:44 +0100
On Wed, 28 Sept 2022 at 15:00, S├ębastien Bini via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, Sep 28, 2022 at 12:52 PM Marcin Jaczewski <
> marcinjaczewski86_at_[hidden]> wrote:
>> Add two callbacks, one with correct index and other for "default"
>> case, then the user can handle it anyway he wants:
>> throw, return default value, terminate or mark it as unreachable code.
> I am not so sure, because then most users will have to supply a no-op
> lambda if they don't care about the error case.
> I think of the following two signatures:
> // Returns whether the integral constant was found
> template <std::size_t N, class Functor>
> bool find_integral_constant(std::size_t index, Functor&& func);
> // Throws std::out_of_range if the integral constant was not found
> template <std::size_t N, class Functor>
> decltype(auto) find_integral_constant(std::size_t index, Functor&& func);
> I prefer the second one as we can retrieve the return value from the
> functor. Other functions in the STL throw exceptions in case of errors, I
> think it's safe to do it in that case as well.

The new hotness is std::expected:

template <std::size_t N, std::invocable<std::integral_constant<std::size_t,
0>> F>
std::expected<std::invoke_result_t<F, std::integral_constant<std::size_t,
0>>, std::size_t>
find_integral_constant(std::size_t index, F&&);

The right (unexpected) alternative would be the same value passed in. You
get *boolean-testable*, exceptions, UB if you want it, and (maybe, maybe
soon) monadics.

Received on 2022-09-28 14:26:57