I know the title may be confusing. I created an "Array" data structure class which has a function called "Iterate". It accepts a function pointer (lambda) and iterates all the nodes with that function. Code:
void Iterate(void(*function)(T* value))
{
if (Empty()) return;
for (Node* iter = start; iter != nullptr; iter = iter->post)
function(iter->value);
}
// Function Call
DataStructure<int> test;
test.Iterate([](int i){ i = 0; });
This function works fine, but sometimes I need some arguments from outside to pass in. It can be done like this:
template<class U>
void Iterate(void(*function)(T* value, U a), U u)
{
if (Empty()) return;
for (Node* iter = start; iter != nullptr; iter = iter->post)
function(iter->value, u);
}
// Function call
DataStructure<int> test;
test.Iterate<float>([](int i, float e){ i = e; }, 10.f);
And it works fine too, but I did not figure out how to do it with "...T". So the function can accept several arguments without having to overload the same function with x templates.
What I tried it:
template<class ...U>
void Iterate(void(*function)(T*, U...), U... u)
{
if (Empty()) return;
for (Node* iter = start; iter != nullptr; iter = iter->post)
function(iter->value, u);
}
But it simply not works. It returns an error:
C++ no instance of overloaded function matches the argument list argument types are: (lambda []void (DataStructureType* data, Arg1 audio, Arg2 dt)->void, Arg1, Arg2)object type is: DataStructure<DataStructureType *>