473,386 Members | 1,763 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,386 software developers and data experts.

Template specialization of a single method from a templated class

I want to specialize a particular function for Integer datatype inside a class template. My XX.h will be
Expand|Select|Wrap|Line Numbers
  1. namespace ZZ
  2. {
  3.        template <class T>
  4.        class XX
  5.        {
  6.            void doSomething(T x);
  7.        };
  8. }
  9.  
Can someone provide me XX.cpp which has template specialization for the function doSomething on Integer datatype.

I tried the following in XX.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "XX.h"
  2. using namespace ZZ;
  3.  
  4. template <class T>
  5. void XX<T>::doSomething(T x)
  6. {
  7.  // Do somtehing with a generic T
  8. }
  9.  
  10. template <>
  11. void XX<int>::doSomething(int v)
  12. {
  13.  // Do somtehing with int
  14. }
  15.  
But this code is not working, can someone help me.
Jan 21 '13 #1

✓ answered by weaknessforcats

Here you go.

This should be in your header file:

Expand|Select|Wrap|Line Numbers
  1. namespace ZZ
  2. {
  3.        template <class T>
  4.        class XX
  5.        {
  6.            void doSomething(T x);
  7.        };
  8. }
  9. template <> void ZZ::XX<int>::doSomething(int x);
  10.  
I added the explicit specialization for the int data type. This tells the compiler to not use the template with int because you have already written that function.

Here is your .cpp:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void ZZ:: XX<T>::doSomething(T x)
  3. {
  4.  // Do somtehing with a generic T
  5. }
  6.  
  7.  
  8. void ZZ:: XX<int>::doSomething(int v)
  9. {
  10.  // Do somtehing with int
  11. }
I had to add that these functions are in the ZZ namespace.

2 5107
weaknessforcats
9,208 Expert Mod 8TB
Here you go.

This should be in your header file:

Expand|Select|Wrap|Line Numbers
  1. namespace ZZ
  2. {
  3.        template <class T>
  4.        class XX
  5.        {
  6.            void doSomething(T x);
  7.        };
  8. }
  9. template <> void ZZ::XX<int>::doSomething(int x);
  10.  
I added the explicit specialization for the int data type. This tells the compiler to not use the template with int because you have already written that function.

Here is your .cpp:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void ZZ:: XX<T>::doSomething(T x)
  3. {
  4.  // Do somtehing with a generic T
  5. }
  6.  
  7.  
  8. void ZZ:: XX<int>::doSomething(int v)
  9. {
  10.  // Do somtehing with int
  11. }
I had to add that these functions are in the ZZ namespace.
Jan 21 '13 #2
Many Many Thanks dude...:)
Jan 22 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
5
by: __PPS__ | last post by:
Hello, I want to write specialized method for a class: template<class A, class B> class xxx{ A a; B b; operator bool()const{ a==b; } };
6
by: __PPS__ | last post by:
it's ok according to the standard to specialize member function like this: template<class T>struct xxx { void func(); }; template<>bool xxx<int>::func(){ //int version } template<>bool...
3
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: ...
3
by: mrstephengross | last post by:
Ok, I've got a templated class Foo: template<typename Tclass Foo { ; }; I want to specialize Foo for vectors holding any type: // Something like: template<typename Cclass Foo<std::vector<C...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
2
by: er | last post by:
hello, here's my problem: struct A{}; struct B{}; struct T{}; struct outer{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.