Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to array

This is probably easy for you but I am not really into references yet. I cannot see why this would not work. Please show me the correct style.

template <typename T>
void shuffle(T (&array)[], size_t len){};

And I try to call it like this:

uint_fast8_t dirBag[4]
Random::shuffle(dirBag, sizeof(dirBag)/sizeof(decltype(dirBag[0])));

The error is:

Error   1   error C2784: 'void Random::shuffle(T (&)[],size_t)' : could not deduce template argument for 'T (&)[]' from 'uint_fast8_t [4]'
like image 665
Etherealone Avatar asked Apr 01 '26 22:04

Etherealone


1 Answers

Try:

template <typename T, size_t N>
void shuffle(T (&array)[N])
{
};

Further, within the body of the function, you have the size of the array: N. If you must pass a size as a parameter, I certainly suppose you can, but the parameter would be independent.


Site Note: Judging by the name of your function, you may also find this of interest:

uint_fast8_t dirBag[4];

// ...populate your array...

std::random_shuffle(std::begin(dirBag), std::end(dirBag));
like image 101
WhozCraig Avatar answered Apr 03 '26 16:04

WhozCraig