473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specialize a template function that contains a template parameter

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

Sep 17 '05 #1
1 1391
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

Sep 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1872
by: Peter Koch Larsen | last post by:
This question has undoubtedly come up zillions of times before, but I've not been able to google for an answer. My problem can be reproduced as follows: template<typename T, bool b> struct bad...
1
1382
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
12
2592
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
3
2900
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
4
3337
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
16
3937
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
1
1409
by: desktop | last post by:
I have stumbled upon different ways to declare a function template specialization. Are the following examples synonymous or are there significant differences: template<typename T // Primary...
7
1906
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
5
3717
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.