Etherealone
Etherealone

Reputation: 3558

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]'

Upvotes: 3

Views: 300

Answers (1)

WhozCraig
WhozCraig

Reputation: 66194

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));

Upvotes: 5

Related Questions