I have a base class with a vector of pointers, and I think to slide the vector outside this class, I wrote the for_each() method:
class model {
static std::vector<model*> models;
...
public:
template <class C>
static void for_each(C *c,void (C::*f)(model *))
{
for (std::vector<model*>::iterator it=models.begin(); it!=models.end(); ++it) {
c->f(*it);
}
...
}
I tryed to use for_each() in a member function:
void v_context::build_cmd()
{
...
auto f1=[&](model* m)->void{commandBuffers.push_back(*(m->build_secondary_buffers(inheritanceInfo, pipelines.gltf)));};
model::for_each<VulkanExample,void (VulkanExample::*)(model*)>(this,f1);
...
}
The compiler (gcc version 10.2.1 20210110 (Debian 10.2.1-6)), rightly returns the error:
error: cannot convert ‘v_context::build_cmd()::<lambda(model*)>’ to
‘void (v_context::)(model)’note: initializing argument 2 of ‘static void model::for_each(C*, void
(C::)(model)) [with C=v_context]’
Is it possible? Does correct syntax template exist?
2
Answers
The simplest is probably to make it even more generic:
Then just capture
[this]
in the lambdas you use it with:Demo
You could use
std::invoke
to implement this logic. A class and a member function pointer this would require swapping the parameters, but otherwise this would allow you to pass any number of additional parameters before themodel*
parameter:Example usages: