Problem with template ctors (smart pointer)

C

Carsten Spieß

Hello all,

i have a problem with a template constructor

I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:

// a base class
class Base{};
// two derived classes
class A : public Base{};
class B : public Base{};

// template smart pointer like (for Base derived classes)
template<class T> class Pointer
{
public:
Pointer(){} // ctor 0
Pointer(T* p){} // ctor 1
Pointer(Base* p){} // ctor 2
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
Pointer(Pointer<T>& p){} // ctor 4

Pointer<T>& operator=(T* p){return *this;}
Pointer<T>& operator=(Base* p){return *this;}
template <class Q> Pointer<T>& operator=(Pointer<Q> &p){return
*this;}
Pointer<T>& operator=(Pointer<T> &p){return *this;}

protected:
T* m_p;
};


my test function which doesn't compile:
void x()
{
Pointer<A> p1;
Pointer<B> p2;
Pointer<A> p3 = p1; // no error
Pointer<A> p4 = p2; // error
Pointer<A> p5 (p2); // no error;
Pointer<A> p6;p6 = p2; // no error
}

I assumed that ctor 3 should be used. But the compiler says:
no matching function for call to `Pointer<A>::pointer (Pointer<A>)'
Pointer<A>::pointer()
Pointer<A>::pointer(A *)
Pointer<A>::pointer(Base *)
Pointer<A>::pointer(Pointer<A> &) <near match>
Pointer<A>::pointer(Pointer<A> &) <near match>

When i remove ctor 4 it compiles, but i need ctor 4.
Did i make an mistake in defining the template ctors 3 or 4?

Thanks for your help, regards
Carsten
 
S

Sharad Kala

Carsten Spieß said:
Hello all,

i have a problem with a template constructor

I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:

// a base class
class Base{};
// two derived classes
class A : public Base{};
class B : public Base{};

// template smart pointer like (for Base derived classes)
template<class T> class Pointer
{
public:
Pointer(){} // ctor 0
Pointer(T* p){} // ctor 1
Pointer(Base* p){} // ctor 2

template <class Q> Pointer(Pointer<Q>& p){} // ctor 3

Change to - template <class Q> Pointer(Pointer<Q> const& p){}

^^^^
Now compiles on Comeau online and g++ 3.3.1

-Sharad
 
V

Vladimir Prus

Carsten said:
Hello all,

i have a problem with a template constructor

I reduced my code to the following (compiled with gcc 2.7.2) to show

gcc 2.72 ?! You really have to upgrade. Get at least 2.95, or better 3.4, or
you'll have a number of problems with standard compilance.
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
Pointer(Pointer<T>& p){} // ctor 4

Pointer<A> p4 = p2; // error
Pointer<A> p5 (p2); // no error;
Pointer<A> p6;p6 = p2; // no error
}

I assumed that ctor 3 should be used. But the compiler says:

gcc 3.3 has no problem with the above, except that ctor 4 should take const
reference. The syntax used in

Pointer<A> p4 = p2; // error

is called copy-initialization. Since p2 is of type Pointer<B>, the compiler
creates temporary of type Pointer<A> (using ctor 3) and then tries to pass
that temporary to copy ctor (ctor 4). The value created by ctor 3 is
temporary, and can't be passed to ctor 4 unless it takes const reference.

And again, upgrade your compiler.

HTH,
Volodya
 
C

Carsten Spieß

Change to - template <class Q> Pointer(Pointer<Q> const& p){}

^^^^
Now compiles on Comeau online and g++ 3.3.1

The sample now compiles too,
but in my real implementation i now get other errors.
I Have to look which are more serious.
Thanks for your help, regards

Carsten
 
C

Carsten Spieß

gcc 2.72 ?! You really have to upgrade. Get at least 2.95, or better 3.4, or
you'll have a number of problems with standard compilance.
I would like to, but it's not possible because in my customer says
i MUST use this compiler.
gcc 3.3 has no problem with the above, except that ctor 4 should take const
reference.
Same as Sharad suggested (for ctor 3).
With a few changes in my code it works now.
The syntax used in

Pointer<A> p4 = p2; // error

is called copy-initialization. Since p2 is of type Pointer<B>, the compiler
creates temporary of type Pointer<A> (using ctor 3) and then tries to pass
that temporary to copy ctor (ctor 4). The value created by ctor 3 is
temporary, and can't be passed to ctor 4 unless it takes const reference.
Thanks for the explanation now i understand where the problem comes
from.
And again, upgrade your compiler.
I can't.
Thank again you helped a lot, regards

Carsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top