jeudi 4 février 2016

How function template non-type parameter is deduced?

I was just coding for fun when I suddenly looked into the code...

template <class T,size_t sz> void my_find(T(&arr)[sz],T val)
{
    cout<<"The size  is "<<sz<<endl;
}
int main()
{
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    my_find(arr,10);
    return 0;
}

And when I compiled and executed it, the output was 10!!!

How did the compiler found out the size without me passing it? I know template function deduce types, but how did this deduce the size?

I am using g++-4.9

Aucun commentaire:

Enregistrer un commentaire