I'm trying to write a C++ function which generates a vector from two calls to an argument function. The argument function accepts an integer pointer and a pointer to an array of elements. If called with nullptr for elements, it will fill the integer with how many elements it has to produce. Then on the second call, it will populate the element memory with that many instances.
So, this pattern can be used to get a vector of elements. I'm repeating this code a lot so I wrote a function which does this for me:
template<typename TElement>
std::vector<TElement> LoadArray(std::function<void(uint32_t*, TElement*)> const func) {
uint32_t count;
func(&count, nullptr);
std::vector<TElement> elements(count);
func(&count, elements.data());
return elements;
}
However, when calling this function, I have to specify TElement in the template as well as the function:
std::vector<Cat> cats = LoadArray<Cat>([](uint32_t* const count, Cat* const cats) {
LoadCats(count, cats);
});
How can I make this type inference automatic in C++20?
Edit: All of the functions have different signatures. Some have return types, others don't; some take an argument before the count and array, some take arguments after.