C++ Logo

sg14

Advanced search

Re: [isocpp-sg14] Introducing `Embedded Function` - A Heap-Free Function Wrapper

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Mon, 17 Aug 2026 23:39:32 -0400
On Mon, Aug 17, 2026 at 8:58 PM Capita Lai via SG14 <sg14_at_[hidden]>
wrote:
> Arthur O'Dwyer via SG14 <sg14_at_[hidden]> 於 2026年8月17日週一 下午11:19寫道:
>>
>> You could "eliminate" it by relaxing the guarantees you document for
your wrapper. Instead of documenting that you always call the stored
callable, you could document that you call "the stored callable, or a copy
of it," and furthermore document that you do not make copies unless the
stored callable is trivially copyable (i.e. you guarantee never to make an
expensive copy).
>
> One possible caveat with the relaxed “stored callable, or a copy of it”
guarantee: if that permission applies to all trivially copyable callables,
I think it can change fairly ordinary stateful-callable semantics. For
example, a mutable lambda capturing an int may be trivially copyable;
invoking a fresh copy each time could produce 1, 1, 1 instead of 1, 2, 3.

Ah, good catch. You'd have to add that if a copy is made, then the copy is
copied back into the original object afterward. So:

    EmptyTrivial t_; // trivially default-constructible, trivially
copyable, and empty
    void operator()() const { auto copy = t_; copy(); *t_ = copy;* } //
what you document

    void operator()() const { EmptyTrivial copy; copy(); } // what you do

–Arthur

Received on 2026-08-18 03:39:51