jeudi 27 août 2015

catch std::function allocations at compile time

I want to only allow use of std::function in my code base if it does not do any allocations.

To this end I can write something like the function below and only use it to create my function instances:

template< typename Functor>
std::function<Functor> makeFunction( Functor f)
{
    return std::function<Functor>(std::allocator_arg, DummyAllocator(), f);
}

where DummyAllocator will assert or throw if it ever gets used at runtime.

Ideally though I would like to catch allocating use cases at compile time.

i.e.

template< typename Functor>
std::function<Functor> makeFunction( Functor f)
{
   static_assert( size needed for function to wrap f < space available in function, 
   "error - function will need to allocate memory");

   return std::function<Functor>(f);
 }

Is something like this possible?

Aucun commentaire:

Enregistrer un commentaire