473,585 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::fi nd(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<MyClas s*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 2436
On 7月1日, 上午2时59分, ".rhavin grobert" <cl...@yahoo.de wrote:
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>::fi nd(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<MyClas s*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.de a 閏rit dans le message de news:
e2************* *************** **...legroup s.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>::fi nd(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<MyClas s*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<typena me T>
typename boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
Do() {return 1;}

//This template function will be called only if T is NOT a pointer. return
type is int
template<typena me T>
typename boost::disable_ if_c<boost::is_ pointer<T>::val ue, int>::type
Do() {return 2;}

template<typena me 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.DoSomethin g() <<endl; // print 2
cout <<a2.DoSomethin g() <<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_i f_c<boost::is_p ointer<T>::valu e, int>::type

there is 2 template arg to enable_if_c
1.boost::is_poi nter<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<typena me T>
typename boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
Do() {return 1;}

//This template function will be called only if T is NOT a pointer.
return type is int
template<typena me T>
typename boost::disable_ if_c<boost::is_ pointer<T>::val ue, int>::type
Do() {return 2;}

template<typena me 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.DoSomethin g() <<endl; // print 2
cout <<a2.DoSomethin g() <<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_i f_c<boost::is_p ointer<T>::valu e, int>::type

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

Sorry, I pushed the wrong button...

ok so boost::is_point er<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<tru e,int>::type == int and enable_if_c<tru e,MyClass>::typ e
== 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_point er<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
7659
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 specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
0
1481
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
2451
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 definition is an "ambiguous template specialization `f<>' for `void f(int, int)' ". I have tested this on a
14
1814
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
9
2756
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 assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
1
2131
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 makeA<int>(void)" (??$makeA@H@@YAPAV?$A@H@@XZ) already defined in testSTL.obj Can you please tell me how i can specialize the template function and still...
2
7683
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
6566
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates...
7
1920
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 specialization). template <typename TOut, typename TIn> void InverseRescaleFunction(TOut *out, const TIn *in, double intercept, double slope, size_t...
7
2548
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 specialization, do I need to implement only the method that is 'different', or do I need to implement all the methods in the class template? ...
0
7904
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main...
0
7835
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8195
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7947
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8209
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5386
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3831
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3856
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1171
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.