By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Problem with template ctors (smart pointer)

P: n/a
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
--
mail<AT>carsten-spiess.de
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Carsten Spieß" <me@privacy.net> wrote in message
news:ip********************************@4ax.com...
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
Jul 22 '05 #2

P: n/a
Carsten Spieß wrote:
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

Jul 22 '05 #3

P: n/a
On Thu, 17 Jun 2004 14:48:42 +0530, Sharad Kala wrote:
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


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
--
mail<AT>carsten-spiess.de
Jul 22 '05 #4

P: n/a
On Thu, 17 Jun 2004 13:42:12 +0400, Vladimir Prus wrote:
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.
HTH,

Thank again you helped a lot, regards

Carsten
--
mail<AT>carsten-spiess.de
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.