Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to do partial specialization template for non member method template

Reply
Thread Tools

how to do partial specialization template for non member method template

 
 
vj
Guest
Posts: n/a
 
      12-20-2010
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




 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      12-20-2010
vj wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[partial] template specialization for a non-member function puzzlecracker C++ 2 01-29-2009 09:19 AM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
member template partial specialization Kai-Uwe Bux C++ 1 12-09-2005 02:45 PM
partial specialization of template member function Levent C++ 5 05-10-2005 09:35 PM
Partial member of class template specialization Petre Iantu C++ 1 08-17-2003 06:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57