lundi 11 février 2019

size of an array with template

Why we need a template for calculating the size of an array why cant i calculate directly with predefined datatypes like int

I have seen the code like

template<typename T,int SIZE>
size_t array_size(const T (&array)[SIZE])
{

    return SIZE;
}

instead of template if we are directly using int why its throwing error

int N;
int size(int (&arr1)[N]) //Passing the array by reference 
{
     return N; //Correctly returns the size too [cool trick ;-)]
}

how its calculating the size

Aucun commentaire:

Enregistrer un commentaire