Re: template syntax to destinguish pointer and objects..?



..rhavin grobert <clqrq@xxxxxxxx> wrote:
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];

You haven't asked about this, but I'm going to comment anyway. I assume
critical_enter() and critical_leave() are entering and leaving a
critical section, and the goal is to have the container thread-safe
(which is usually misguided, but I won't concentrate on that). However,
you return a reference to an element inside the vector, and access to
this reference is not in any way protected. This reference may become
invalid as soon as the function returns, when another thread adds an
element and the vector has to grow and reallocate its memory.

Besides, std::vector itself is not thread-safe. You don't put, say,
m_vec[iIndex] call inside a critical section: it may break if another
thread modifies the vector at the same time.

...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?

You don't need to do anything special - just don't call it. If you never
call a method of a template, it is never instantiated.

I tried to add the following function
______________________________________

template <class T, typename I>
T& CQVector<T,I>::add(T* t)

If T is a pointer type (e.g. int*), this method accepts a double pointer
as a parameter (int**). Is that what you want? Do you expect to pass
double pointers this way?

...and used the following scenario to test it...
______________________________________

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, int> tv1;
CQVector<test*, int> tv2;
______________________________________

but then the first vector uses that newly added function

What do you mean, uses? The code above doesn't call any flavor of add(),
as far as I can tell.

so _how_ do i put this in correct syntax?

Didn't you say that you don't need to call add() on a CQVector of
pointers? If you never call it, why do you care about its syntax?

if CQVector<obj, x> use T& CQVector<T,I>::add(T const& t)
if CQVector<obj*, x> use T& CQVector<T,I>::add(T* const t)

Why? Do you plan to pass obj** to CQVector<obj*, x>::add ?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: template specialization bug
    ... C2764 "template parameter T not used in partial specialization" with ... Say you want to use a container to store objects of type LargeType. ... you want to store pointers to LargeType (large ... A type traits class contains information about a certain type. ...
    (microsoft.public.vc.language)
  • Re: A question to an expert of C++ templates
    ... > Your original design has a problem in that linkage pointers point to Nodes ... > pointers can access Node members but cannot readily acess Linkage members ... > into template classes and corrects your syntax for the Link template ...
    (microsoft.public.vc.language)
  • Re: template syntax to destinguish pointer and objects..?
    ... std::vector itself is not thread-safe. ... CQVector of pointers? ... i just try to compile the posted test. ... If T is a pointer type, this method accepts a double pointer ...
    (microsoft.public.vc.language)
  • Re: [C++] Help with a Database for User Defined Classes
    ... > class Data { ... What does the template expand into? ... template< class Datum*> ... You vector doesn't hold pointers. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Creating a custom project in VC++ .NET 2003
    ... > Now I'm thinking of creating a wizard or template for future projects. ... > Care to give an pointers on how to do this? ...
    (microsoft.public.vc.language)

Loading