0

This is a follow up question to : this question - replacing an std::function with a non-allocating one , that does not use std .

Was wondering if there is something more robust that can replace: std::aligned_union

 typedef unsigned char callable_array[100];
 typename std::aligned_union<0, callable_array, CallableT<void(*)()>, CallableT<void (CallableT<void(*)()>::*)()>>::type callable_;
                                        

The suggested code to do that is causing an compilation error

CallableT<decltype([]()->void {})>, // error: lambda expression in an unevaluated operand

trying both c++11 and c++14

It also uses std::max - was hoping for some ideas on how to replace it as well.

Edit: my example that shows compilation error : Example that don't compile

schanti schul
  • 681
  • 3
  • 14
  • "*It also uses std::max - was hoping for some ideas on how to replace it as well.*" I can understand wanting to avoid `std::function`'s allocations, but `max` doesn't allocate. Is there some reason to avoid it? – Nicol Bolas Aug 29 '22 at 22:34
  • The question you linked has an answer with a [godbolt](https://godbolt.org/g/w2cEaq) link, is that not good enough? Uses placement new, no allocations. Lambda parameter also compiles. – Ave Milia Aug 29 '22 at 23:17
  • I have built a small example using the linked code, It wont compile. (edited the example into the question) – schanti schul Aug 29 '22 at 23:41
  • I am working on an embedded system with restrictions – schanti schul Aug 29 '22 at 23:42
  • Add `static auto MakeLambda() { return []()->void {}; }` to the class, and use `decltype(MakeLambda())` where you need the lambda type. [Demo](https://gcc.godbolt.org/z/336TEoczo). It's unclear though why you think the size or alignment of that lambda type is indicative of anything. Clearly a lambda that captures a large object by value can be arbitrarily large. – Igor Tandetnik Aug 31 '22 at 01:04
  • Thanks, should I better use "etl::uninitialized_buffer" from : https://github.com/ETLCPP/etl/blob/master/include/etl/memory.h – schanti schul Aug 31 '22 at 01:44
  • Also this "hack" to tell how many bytes were allocated? https://stackoverflow.com/questions/46163607/avoid-memory-allocation-with-stdfunction-and-member-function – schanti schul Aug 31 '22 at 02:00

0 Answers0