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

template specialization for pointer-to-type

guess you have the following:

_________________________________________________
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_vec;
};

template <class T>
int CQVector<T>::find(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;
}
_________________________________________________

this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.
now i also want the possibility to store pointers, eg

CQVector<MyClass*m_foo;

and need some specialisation that does a...
_________________________________________________

int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_________________________________________________

what syntax is need ed for the specialisation?
something like template<class*Tdoesnt work...

TIA, -.rhavin;)
Jun 30 '08 #1
3 2422
On 7ÔÂ1ÈÕ, ÉÏÎç2ʱ59·Ö, ".rhavin grobert" <cl...@yahoo.dewrote:
guess you have the following:

_________________________________________________
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_vec;

};

template <class T>
int CQVector<T>::find(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;}

_________________________________________________

this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.

now i also want the possibility to store pointers, eg

CQVector<MyClass*m_foo;

and need some specialisation that does a...
_________________________________________________

int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_________________________________________________

what syntax is need ed for the specialisation?
something like template<class*Tdoesnt work...

template <class T>
class CQVector<T*>
{
...
};
Jul 1 '08 #2

".rhavin grobert" <cl***@yahoo.dea écrit dans le message de news:
e2**********************************...oglegroups.com...
guess you have the following:

_________________________________________________
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_vec;
};

template <class T>
int CQVector<T>::find(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;
}
_________________________________________________

this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.
now i also want the possibility to store pointers, eg

CQVector<MyClass*m_foo;

and need some specialisation that does a...
_________________________________________________

int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_________________________________________________

what syntax is need ed for the specialisation?
something like template<class*Tdoesnt work...

TIA, -.rhavin;)

No need to partially specialize your class by the way. Here an example.
#include <boost/utility.hpp>
#include <boost/type_traits.hpp>

using namespace std;

//This template function will be called only if T is a pointer. return type
is int
template<typename T>
typename boost::enable_if_c<boost::is_pointer<T>::value, int>::type
Do() {return 1;}

//This template function will be called only if T is NOT a pointer. return
type is int
template<typename T>
typename boost::disable_if_c<boost::is_pointer<T>::value, int>::type
Do() {return 2;}

template<typename T>
class A
{
public:
int DoSomething()
{
return Do<T>(); // Do actually take careof doing the right thing
depending on T
}
};

int main()
{
A<inta1;
A<int*a2;
cout <<a1.DoSomething() <<endl; // print 2
cout <<a2.DoSomething() <<endl; // print 1
return 0;
}

Ok I agree that the Do<Tfunction template could look a little (ok maybe
not just a little...) strange at first sight but it is not actually that
bad.

take for example

boost::enable_if_c<boost::is_pointer<T>::value, int>::type

there is 2 template arg to enable_if_c
1.boost::is_pointer<T>::value
2. int


Jul 1 '08 #3
>
No need to partially specialize your class by the way. Here an example.
#include <boost/utility.hpp>
#include <boost/type_traits.hpp>

using namespace std;

//This template function will be called only if T is a pointer. return
type is int
template<typename T>
typename boost::enable_if_c<boost::is_pointer<T>::value, int>::type
Do() {return 1;}

//This template function will be called only if T is NOT a pointer.
return type is int
template<typename T>
typename boost::disable_if_c<boost::is_pointer<T>::value, int>::type
Do() {return 2;}

template<typename T>
class A
{
public:
int DoSomething()
{
return Do<T>(); // Do actually take careof doing the right thing
depending on T
}
};

int main()
{
A<inta1;
A<int*a2;
cout <<a1.DoSomething() <<endl; // print 2
cout <<a2.DoSomething() <<endl; // print 1
return 0;
}

Ok I agree that the Do<Tfunction template could look a little (ok maybe
not just a little...) strange at first sight but it is not actually that
bad.

take for example

boost::enable_if_c<boost::is_pointer<T>::value, int>::type

there is 2 template arg to enable_if_c
1.boost::is_pointer<T>::value
2. int

Sorry, I pushed the wrong button...

ok so boost::is_pointer<T>::value will evaluate to true if T is a pointer
and false otherwise.
Inside enable_if_c, there is a simple typedef:
typedef T type;
it happen thet the second argument of the enable_if_c template is T (in our
case int)
so enable_if_c<true,int>::type == int and enable_if_c<true,MyClass>::type
== MyClass, and so on

So we can use the enable_if_c template as the return argument four our Do
function template.

Now what haapen when boost::is_pointer<T>::value evaluate to false? It is a
substitution failure (see the SFINAE principle). In short the compiler will
just reject the template and search for a better match (ok, if he does not
find a better match he will give you an error). In our case, the second Do
function will match for every non pointer type.

In your case, you seem to have only 1 function to "specialize"and it it is a
really simple function, so if you can install boost (www.boost.org) you
should consider this option.

----------------------
Eric Pruneau
Jul 1 '08 #4

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

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
0
by: Patrick Kowalzick | last post by:
Dear all, the following code is illegeal (but compiles with MSVC 7.1): // *** illegal *** struct outer0 { template<typename inner_var> struct inner { }; template<>
1
by: Rafal Dabrowa | last post by:
Consider the following code: template <class T> void f(T, int) {} template <class T> void f(int, T) {} template<> void f(int, int) {} // ambiguous ? My compiler complains that the last...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
1
by: Bari | last post by:
Hi, When i do the following specializition which commented in the source then including the header in two cpp files gives me an error: test3.obj : error LNK2005: "class A<int* __cdecl...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
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...
7
by: (2b|!2b)==? | last post by:
I have a class template. Each of the instantiations implements a method in the class template differently, so I (need?) to use template speciaization. My question is this, when writing the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.