I wrote a simple program that sends array array[]
to a function named ARRAY_MAN, which then modifies the array's contents:
#include <vector>
#include <iostream>
template <class Var, class T, std::size_t N, class... Args>
void ARRAY_MAN(Var variable, T(&)[N], uint32_t numParams, Args... args)
{
std::vector<T> arguments{args...};
if(arguments.size() != numParams)
throw std::runtime_error("mismatch");
for(uint32_t i = 0; i < numParams; ++i)
variable[i] = arguments[i];
}
int main(int argc, char* argv[])
{
int array[] = {1,2,3}; // (*)
// print array before sending
for(uint32_t i = 0; i < 3; ++i)
std::cout << array[i] << " ";
std::cout << std::endl;
ARRAY_MAN(array, array, 3, 34, 56, 10);
// print array after sending
for(uint32_t i = 0; i < 3; ++i)
std::cout << array[i] << " ";
std::cout << std::endl;
return 0;
}
This compiles and runs. But if I replace the line marked (*)
with this line:
int *array = new int(3);
I get this error:
no matching function for call to ‘ARRAY_MAN(int*&, int*&, int, int, int, int)’
How can I send this second array
to the ARRAY_MAN function?