Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > specialize a template function that contains a template parameter

Reply
Thread Tools

specialize a template function that contains a template parameter

 
 
sebastian
Guest
Posts: n/a
 
      09-17-2005
Hi,
I'd like to specialize a template function that contains a template
parameter. In Example i have the following function declared:

....

template < int i > static stupid_object& doSomething( int i,
const another_stupid_object& __aso);

....

and now i would like to specialize this function for i equals 6.
How can i realize this ?:

....
template<> stupid_object& doSomething( ???? ) {
....
}
....

In the broader sens what happens if the template-argument is not of a
simple type like "int" but an own declared object (i.E. "own_object").
Which operators do i have to overwrite ? Is there any documentation about
this ?
Unfortunately Stroustrup doesn't write anything about this case. Is
it generally realizable/implementable ???

Thanx in advance
cheers
sebastian

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      09-17-2005
sebastian wrote:
> Hi,
> I'd like to specialize a template function that contains a template
> parameter. In Example i have the following function declared:
>
> ...
>
> template < int i > static stupid_object& doSomething( int i,
> const another_stupid_object& __aso);
>
> ...
>
> and now i would like to specialize this function for i equals 6.
> How can i realize this ?:
>
> ...
> template<> stupid_object& doSomething( ???? ) {
> ...
> }
> ...
>
> In the broader sens what happens if the template-argument is not of a
> simple type like "int" but an own declared object (i.E. "own_object").
> Which operators do i have to overwrite ? Is there any documentation about
> this ?
> Unfortunately Stroustrup doesn't write anything about this case. Is
> it generally realizable/implementable ???
>
> Thanx in advance
> cheers
> sebastian


It is not possible for the compiler to deduce a non-type template
parameter for a function template. To do so, the compiler would have to
know which calls to that function passed the value 6, and also be sure
that the value passed was always 6. Since it can do neither, the
compiler requires that every call to doSomething specifies the non-type
parameter:

doSomething<6>( i, a);

Of course, this syntax is no better than simply writing a function
called doSomething6. And for that reason function templates are rarely
declared with non-type parameters. They just are not very useful.

But to answer the question, a function template with an int
parameterized type can be specialized for the value 6 in this way:

template <>
int DoSomething<6>(int i, someType s)
{
...

Greg

 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How to partial specialize a template member function in a template class? PengYu.UT@gmail.com C++ 16 03-15-2007 02:16 PM
specialize template member function of a template class toton C++ 3 01-25-2007 02:25 PM
How to partially specialize a class but NOT specialize a member function: mrstephengross C++ 1 08-02-2005 07:44 PM
Partially specialize a template with another template class Old Wolf C++ 4 04-08-2005 03:53 PM



Advertisments