Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with template ctors (smart pointer)

Carsten Spieß
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Sharad Kala
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Problem with template ctors (smart pointer)



"Carsten Spieß" <me@privacy.net> wrote in message
news:ipj2d01jogga8903sbri217q2cn6rpbniq@4ax.com...[color=blue]
> 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[/color]

[color=blue]
> template <class Q> Pointer(Pointer<Q>& p){} // ctor 3[/color]

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

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

-Sharad


Vladimir Prus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Problem with template ctors (smart pointer)


Carsten Spieß wrote:
[color=blue]
> 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[/color]

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.
[color=blue]
> template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
> Pointer(Pointer<T>& p){} // ctor 4[/color]

[color=blue]
> 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:[/color]

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

Carsten Spieß
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Problem with template ctors (smart pointer)


On Thu, 17 Jun 2004 14:48:42 +0530, Sharad Kala wrote:
[color=blue][color=green]
>> template <class Q> Pointer(Pointer<Q>& p){} // ctor 3[/color]
>
>Change to - template <class Q> Pointer(Pointer<Q> const& p){}
>
>^^^^
>Now compiles on Comeau online and g++ 3.3.1[/color]

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
Carsten Spieß
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Problem with template ctors (smart pointer)


On Thu, 17 Jun 2004 13:42:12 +0400, Vladimir Prus wrote:
[color=blue]
>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.[/color]
I would like to, but it's not possible because in my customer says
i MUST use this compiler.
[color=blue]
>gcc 3.3 has no problem with the above, except that ctor 4 should take const
>reference.[/color]
Same as Sharad suggested (for ctor 3).
With a few changes in my code it works now.
[color=blue]
> 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.[/color]
Thanks for the explanation now i understand where the problem comes
from.
[color=blue]
>And again, upgrade your compiler.[/color]
I can't.
[color=blue]
>HTH,[/color]
Thank again you helped a lot, regards

Carsten
--
mail<AT>carsten-spiess.de
Closed Thread