Instead of a type-erased function, should the repeat_directive take the callbacks as template arguments? That would allow for some inlining/optimizations by the compiler. This may require separate static functions that create and return the lambdas, so the type can be determined with declval.