C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::constant_size_function

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 27 Jun 2024 13:48:49 +0100
On Mon, Jun 17, 2024 at 3:02 PM Lénárd Szolnoki wrote:
>
> > Not sure what you mean here. Could you please provide a code snippet
> > or a godbolt?
>
> https://godbolt.org/z/5GsY86f6Y


Thanks for that. I've tried to optimise it as follows for
functions/functors that don't have parameters:

      static constexpr decltype(auto) GetLambda(F_ref f,
Params_refs... args) noexcept
      {
          typedef remove_reference_t<F_ref> F;
          typedef remove_pointer_t<F> funcF; // Might be
pointer-to-func or ref-to-func
          if constexpr ( sizeof...(Params_refs) )
          {
              // Next line returns lambda by value
              return [&f,&args...](void) noexcept(excepts) -> decltype(auto)
                {
                  return invoke( static_cast<F_ref>(f),
static_cast<Params_refs>(args)... );
                };
          }
          else if constexpr ( is_function_v<funcF> )
          {
              return static_cast<funcF*>(f); // returns
pointer-to-func by value
          }
          else if constexpr ( is_trivial_v<F> && sizeof(F)<=sizeof(void*) )
          {
              return static_cast<F>(f); // returns small functor
object by value
          }
          else
          {
              return static_cast<F_ref>(f); // returns a ref to functor object
          }
      }

Torture-tested on GodBolt:

      https://godbolt.org/z/o86nG1z4h

And I've updated P3288 to Revision No. 3 accordingly:

      http://www.virjacode.com/papers/p3288r3.htm

Received on 2024-06-27 12:49:02