473,809 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template; how to dont instantiate a function if type is a pointer..?

hello;-)

i have that following little template that defines some type of
vector.
it works with structs and i want to use it also for simple pointers.

the problem is, in following function...
_______________ _______________ ________

template <class T, typename I>
T& CQVector<T,I>:: add(I id)
{
critical_enter( );
int iIndex = find_id(id);
if (iIndex < 0)
{
T t(id); // **** <-- here ****
m_vec.push_back (t);
iIndex = m_vec.size() - 1;
}
critical_leave( );
return m_vec[iIndex];
}

_______________ _______________ ________

....the line T t(id); doenst work, what simply doesnt matter, because
that whole function is never needed if i have CQVector<some*> . So, how
do i tell the compiler to please dont try to compile it IF we have a
CQVector of pointers?

here is the whole template...

_______________ _______________ ________

//
*************** *************** *************** *************** *************** *
// * template class CQVector
declaration *
//
*************** *************** *************** *************** *************** *

template <
class T, // class of comparable (op<, op==) elements to host
in QVector
typename I=int // type of ID, must be accessible by elements public
ID()-func
>
class CQVector
{
public:
//
------------------------------------------------------------------------
// construction and destruction

// default constructor
CQVector();
// default destructor
virtual ~CQVector();

//
------------------------------------------------------------------------
// special vector functions

// start critical access
inline void critical_enter( ) const
{EnterCriticalS ection(&m_Crit) ;};
// end critical access
inline void critical_leave( ) const
{LeaveCriticalS ection(&m_Crit) ;};
// set invalid element
virtual inline void invalid(T const& invalid) {m_invalid =
invalid;};

//
------------------------------------------------------------------------
// vector informative functions

// size of vector
inline UINT size() const {return
m_vec.size();};
// constant ref to inalid element
inline T const& invalid() const {return
m_invalid;};
// ref to invalid element thru null-element
inline T& null() {m_null = m_inv; return
m_null;};
// find an element, returns index or -1 if none is found
virtual int find(T const& t) const;
// find an element by its ID, returns index or -1 if none is found
virtual int find_id(I const& i) const;

//
------------------------------------------------------------------------
// single element access (external critical sync needed!)

// get const ref
T const& operator[] (UINT nIndex) const;
// get ref
T& operator[] (UINT nIndex);
// get const ref to element of given ID
T const& operator() (I const& i) const;
// get const ref to element of given ID
T& operator() (I const& i);

//
------------------------------------------------------------------------
// removal of elements

// remove element of given index from vector
virtual bool remove(UINT nIndex);
// clear registry, remove all elements
virtual void clear();
// remove element of given ID from vector
bool remove_id(I id);
// remove an element from the vector
bool remove_obj(T const& t);

//
------------------------------------------------------------------------
// insert and adding of elements

// add an element to the vector
virtual T& add(T const& t);
// add an element to the vector
virtual T& add(I id);

private:
std::vector<T m_vec;
T m_invalid;
T m_null;
mutable CRITICAL_SECTIO N m_Crit;
mutable I m_idLast;
mutable bool m_fLastValid;
mutable UINT m_nLastIdx;
I _ID(T* const& pt) const {return pt->ID();};
I _ID(T const& t) const {return t.ID();};
};
//
*************** *************** *************** *************** *************** *
// * template class CQVector
definition *
//
*************** *************** *************** *************** *************** *

//-----------------------------------------------------------------------------
// default constructor
template <class T, typename I>
CQVector<T,I>:: CQVector()
{
m_fLastValid = false;
InitializeCriti calSection(&m_C rit);
}

//-----------------------------------------------------------------------------
// default destructor
template <class T, typename I>
CQVector<T,I>:: ~CQVector()
{
clear();
DeleteCriticalS ection(&m_Crit) ;
}

//-----------------------------------------------------------------------------
// clear registry
template <class T, typename I>
void CQVector<T,I>:: clear()
{
critical_enter( );
m_vec.clear();
m_fLastValid = false;
critical_leave( );
}

//-----------------------------------------------------------------------------
// get const ref to item (sync_extern!)
template <class T, typename I>
T const& CQVector<T,I>:: operator[](UINT nIndex) const
{
if (nIndex < m_vec.size())
return m_vec[nIndex];
return m_invalid;
}

//-----------------------------------------------------------------------------
// get ref to item (sync extern!)
template <class T, typename I>
T& CQVector<T,I>:: operator[](UINT nIndex)
{
if (nIndex < m_vec.size())
return m_vec[nIndex];
m_null = m_invalid;
return m_null;
}

//-----------------------------------------------------------------------------
// add an element to the vector
template <class T, typename I>
T& CQVector<T,I>:: add(T const& t)
{
critical_enter( );
m_vec.push_back (t);
critical_leave( );
return m_vec.back();
}

//-----------------------------------------------------------------------------
// add an element to the vector
template <class T, typename I>
T& CQVector<T,I>:: add(I id)
{
critical_enter( );
int iIndex = find_id(id);
if (iIndex < 0)
{
T t(id);
m_vec.push_back (t);
iIndex = m_vec.size() - 1;
}
critical_leave( );
return m_vec[iIndex];
}

//-----------------------------------------------------------------------------
// remove element of given index from vector
template <class T, typename I>
bool CQVector<T,I>:: remove(UINT nIndex)
{
critical_enter( );
if (nIndex >= m_vec.size())
{
critical_leave( );
return false;
}
m_vec.erase(m_v ec.begin() + nIndex);

if (m_fLastValid)
{
if (m_nLastIdx == nIndex)
m_fLastValid = false;
if (m_nLastIdx nIndex)
m_nLastIdx--;
}

critical_leave( );
return true;
}

//-----------------------------------------------------------------------------
// find an element, returns index or -1 if none is found
template <class T, typename I>
int CQVector<T,I>:: find(T const& t) const
{
critical_enter( );
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt] == t)
break;
}
critical_leave( );
return iCnt;
}

//-----------------------------------------------------------------------------
// find an element, returns index or -1 if none is found
template <class T, typename I>
int CQVector<T,I>:: find_id(I const& i) const
{
critical_enter( );
if (m_fLastValid)
{
if (m_idLast == i)
{
return m_nLastIdx;
critical_leave( );
}
}

int iCnt = m_vec.size();
while (iCnt-->0)
{
if (_ID(m_vec[iCnt]) == i)
{
m_fLastValid = true;
m_nLastIdx = iCnt;
break;
}
}
critical_leave( );
return iCnt;
}

//-----------------------------------------------------------------------------
// get const ref to element of given ID
template <class T, typename I>
T const& CQVector<T,I>:: operator() (I const& i) const
{
int i = find_id(i);
if (i < 0)
return m_invalid;
return m_vec[i];
}

//-----------------------------------------------------------------------------
// get const ref to element of given ID
template <class T, typename I>
T& CQVector<T,I>:: operator() (I const& i)
{
int idx = find_id(i);
if (idx < 0)
{
m_null = m_invalid;
return m_null;
}
return m_vec[idx];
}

//-----------------------------------------------------------------------------
// remove element of given ID from vector
template <class T, typename I>
bool CQVector<T,I>:: remove_id(I id)
{
critical_enter( );
int i = find_id(i);
if (i >= 0)
remove(i);
critical_leave( );
return (i>=0);
}

//-----------------------------------------------------------------------------
// remove an element to the vector
template <class T, typename I>
bool CQVector<T,I>:: remove_obj(T const& t)
{
critical_enter( );
int i = find(t);
if (i >= 0)
remove(i);
critical_leave( );
return (i>=0);
}
Jul 2 '08 #1
2 1408
..rhavin grobert wrote:
So, how
do i tell the compiler to please dont try to compile it IF we have a
CQVector of pointers?
You make a specialization of that template function for the case where
T is a pointer, and do nothing (or whatever appropriate).

The compiler will always choose the template which most closely
matches the parameter type, so if you specialize for a pointer type (any
pointer type) the compiler will use that when you use any pointer.
Jul 2 '08 #2
On 2 Jul., 18:30, Juha Nieminen <nos...@thanks. invalidwrote:
.rhavin grobert wrote:
So, how
do i tell the compiler to please dont try to compile it IF we have a
CQVector of pointers?

You make a specialization of that template function for the case where
T is a pointer, and do nothing (or whatever appropriate).

The compiler will always choose the template which most closely
matches the parameter type, so if you specialize for a pointer type (any
pointer type) the compiler will use that when you use any pointer.
yes, _but how_ ?
in ....
_______________ _______________ ______

#include "QVector.hp p"
struct test {
test(int = 0) {};
bool operator==(test const&) const {return false;};
bool operator<(test const&) const {return false;};
int ID() const {return 1;};
};
CQVector<test, inttv1;
CQVector<test*, inttv2;
_______________ _______________ ______

when i add the following fn to the template
_______________ _______________ ______

template <class T, typename I>
T& CQVector<T,I>:: add(T* const t)
{
critical_enter( );
m_vec.push_back (t);
critical_leave( );
return m_vec.back();
}
_______________ ___________

then the first vector uses that newly added (what is wrong, he should
use

T& CQVector<T,I>:: add(T const& t)

because he has objects, and the second bector whitch has pointers
should use that newly added

T& CQVector<T,I>:: add(T* const t)

but still uses the fn

T& CQVector<T,I>:: add(I id)

so _how_ do i put this in correct syntax?
if CQVector<obj, xuse T& CQVector<T,I>:: add(T const& t)
if CQVector<obj*, xuse T& CQVector<T,I>:: add(T* const t)
just use T& CQVector<T,I>:: add(I id) if someone does a

myVector.add(x) ;

?

TIA, -.rhavin;)
Jul 2 '08 #3

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

Similar topics

17
2045
by: Jacek Dziedzic | last post by:
Hello! I have a templated class that serves as a simple vector of elements. template <typename T> class simple_vector : public math_object { // ... lots of simple_vector operations // the trouble begins here:
8
11369
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
4
2343
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a .h and a .cpp file, gcc throws some strange linking errors. Compiling like this:
10
2100
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ========================= template <class T> myClass {....} ;
5
2025
by: truce | last post by:
I searched but couldn't find anything _quite_ like this. I have some code I inherited that needs some work. Basically I have a template that is instantiated in the c++ code and so only the headers are distributed. That works great, the only problem is that I cant get the friend ostream inserter operator to work correctly. Basically it never is instantiated so it never shows up in the library. Here is a (close to) minimum example...
4
2295
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
4
2377
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
2
6652
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
9721
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’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.