Variadic template with function argument I am trying to develop an ECS registry. How would you improve this ? First disadvantage comes to my mind is using lambdas might be expensive, however I was not able to replicate functionality using F&amp;&amp; template arguments.

    engine-&gt;addEntity(1, 
        Position{1, 1},
        Mesh{"thing.gltf"},
        PhysicsBody{10.0f}
    );

    engine-&gt;addEntity(2,
        Position{ 2, 2 },
        Mesh{ "other.gltf" },
        PhysicsBody{ 30.0f }
    );

    engine-&gt;entity&lt;Position&gt;(1, [&amp;](auto pos) {
        std::cout &lt;&lt; "entity";
    });
    engine-&gt;each&lt;Position, Mesh&gt;([] (int id, Position&amp; pos, Mesh&amp; mesh) {
        std::cout &lt;&lt; "entity" &lt;&lt; id;
    });


    template&lt;typename First&gt;
    void entity(int id, std::function&lt;void(First&amp;)&gt; callback)
    {
        auto comp = storage&lt;First&gt;()-&gt;get(id);
        if (!comp) return;
        callback(*comp);
    }

    template&lt;typename First, typename...Rest&gt;
    void entity(int id, std::function&lt;void(Rest&amp;...)&gt; callback)
    {
        auto comp = storage&lt;First&gt;()-&gt;get(id);
        if (!comp) return;
        entity&lt;Rest...&gt;(id, [](Rest&amp;...args) {
            callback(*comp, args...)
        });
    }

    template&lt;typename T, typename...Rest, typename F&gt;
    void each(F&amp;&amp; callback)
    {
        storage&lt;T&gt;()-&gt;forEach([&amp;](int id, T&amp; value) {
            entity&lt;Rest...&gt;(id, [&amp;](Rest&amp;...args) {
                callback(id, value, args...);
            });
        });
    }
using lambdas might be expensive


Why do you think that?

Lambdas are simply syntactic hand waving for class functors (that have been around for over 3 decades). Functors are considered (in general) to be much more efficient than standard functions (especially when used by templates) as the compiler can do a lot of optimizations on them.

auto x = [&amp;capture](X const&amp; parm){ /* BODY */ }


You can consider as syntactic sugar for:

class CompilerNamedType
{
    TypeOfCapure&amp;     capture;
    public:
        CompilerNamedType(TypeOfCapure&amp; capture)
            : capture(capture)
        {}

        CompilerCalculatedReturnType operator()(X const&amp; param) const
        {
            /* BODY */
        }
};

CompilerNamedType  x(capture);