how to do partial specialization template for non member method template

V

vj

Hi All,

I was working on a dummy routine to fill up an array with random
numbers using function templates but jst hit a road block. I jst cant
get the code below to compile with MSVC 2010 compiler. I m not sure if
i m using the correct template specialization syntax here. So please
provide me some insight as to what is wrong here:

<code>
template <int * arr, int size> void randfill()
{
arr[size]=rand();
randfill<arr,size-1>();
}

template<int * arr> void randfill<arr,0>()
{
return;
}//<!-- error is here

//template instantiation code
int arr[10];
randfill<arr,10>();
</code>

the compilation error i m getting is stl.cpp(35) : error C2768:
'randfill' : illegal use of explicit template arguments.

Thanks in advance,
VJ
 
M

Marc

vj said:
I was working on a dummy routine to fill up an array with random
numbers using function templates but jst hit a road block. I jst cant
get the code below to compile with MSVC 2010 compiler. I m not sure if
i m using the correct template specialization syntax here. So please
provide me some insight as to what is wrong here:

<code>
template <int * arr, int size> void randfill()
{
arr[size]=rand();
randfill<arr,size-1>();
}

template<int * arr> void randfill<arr,0>()
{
return;
}//<!-- error is here

//template instantiation code
int arr[10];
randfill<arr,10>();
</code>

the compilation error i m getting is stl.cpp(35) : error C2768:
'randfill' : illegal use of explicit template arguments.

Your indices are off by 1.

Why do you want to make arr a template parameter instead of a regular
function argument?

Functions don't have partial specialization, only full specialization
and overloading. One common workaround is to use a class instead.
Another one is to pass you randfill function an argument of type
integral_constant<int,size> and overload.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top