473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to specialize a function template correctly?

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 function template
void myTemp(T) {

}
template< // int version with arg 'a'
void myTemp(int a) {
// do something with a
}
template< // int type only version
void myTemp(int) {

}
template< // int int type version
void myTemp<int>(int) {

}
May 12 '07 #1
1 1403
desktop wrote:
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 function template
void myTemp(T) {

}
template< // int version with arg 'a'
void myTemp(int a) {
// do something with a
}
template< // int type only version
void myTemp(int) {

}
This happens with every function parameter. If you are not going to use
it in the function implementation, you can omit the name of the
parameter, leaving only the type. It's also common practice in order to
avoid warnings referring to the unused variable "a" (if you don't use
it, of course :)).

>
template< // int int type version
void myTemp<int>(int) {

}
This is an explicit specialization, while the previous ones are
implicit. The point here is that, as every time in which you can deduce
the template parameters from the function arguments, you don't need to
write them explicitly.

If I can give you a suggestion, a lot of the things that you're asking
are covered in much more extent and accuracy in the chapter 13.5 of the
Stroustrup.

Regards,

Zeppe
May 12 '07 #2

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

Similar topics

4
by: Old Wolf | last post by:
I have a template class like this: template<typename T, int N> struct Foo { ........... }; which I have successfully specialized for some types, eg:
1
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: ...
1
by: sebastian | last post by:
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&...
3
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask,...
3
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
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
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 {
7
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.