Topic: template copy constructor ignored?
Author: viisi@chello.at (Andi Riedl)
Date: Fri, 15 Jun 2001 04:50:44 GMT Raw View
hi!
after some longer debug session i became aware of some
ill looking behaviour of gcc (2.95 & 2.97) and would
like to know if this really is violating the standard.
given the following code:
#v+
#include <iostream>
template <bool R>
class Foo
{
public:
Foo(int x):
x(x)
{ report ("Foo(int)"); }
~Foo()
{ report ("~Foo()"); }
template <bool Rsrc>
Foo(const Foo<Rsrc>& src):
x(src.x)
{ report ("Foo(const Foo&)"); }
template <bool Rsrc>
void operator= (const Foo<Rsrc>& src)
{ x=src.x; report("operator="); }
void report(const char* fname)
{
std::cout << fname << " x=" << x << std::endl;
}
int x;
};
int main()
{
Foo<false> foo1(1);
Foo<true> foo2(2);
Foo<false> foo3(foo1);
Foo<false> foo4(foo2);
Foo<false> foo5(5);
foo5 = foo1;
foo5 = foo2;
return 0;
}
#v-
i get this output:
#v+
Foo(int) x=1
Foo(int) x=2
Foo(const Foo&) x=2
Foo(int) x=5
operator= x=2
~Foo() x=2
~Foo() x=2
~Foo() x=1
~Foo() x=2
~Foo() x=1
#v-
the compile doesn't recognize my copy constructor
Foo<false>::Foo(const Foo<false>&);
to set up 'foo3', neither my operator=
void Foo<false>::operator= (const Foo<false>&);
to assign foo1 to foo5.
instead, it uses it's own auto-generated ones.
can you tell me if there is any reason for this?
cu, andi
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Fri, 15 Jun 2001 16:27:18 GMT Raw View
"Andi Riedl" <viisi@chello.at> wrote in message
news:slrn9iitmf.647.viisi@elcher.stranitzky.org...
> hi!
>
> after some longer debug session i became aware of some
> ill looking behaviour of gcc (2.95 & 2.97) and would
> like to know if this really is violating the standard.
>
> given the following code:
>
> #v+
>
> #include <iostream>
>
> template <bool R>
> class Foo
> {
> public:
> Foo(int x):
> x(x)
> { report ("Foo(int)"); }
>
> ~Foo()
> { report ("~Foo()"); }
>
> template <bool Rsrc>
> Foo(const Foo<Rsrc>& src):
> x(src.x)
> { report ("Foo(const Foo&)"); }
>
> template <bool Rsrc>
> void operator= (const Foo<Rsrc>& src)
> { x=src.x; report("operator="); }
>
> void report(const char* fname)
> {
> std::cout << fname << " x=" << x << std::endl;
> }
>
> int x;
> };
>
> int main()
> {
> Foo<false> foo1(1);
> Foo<true> foo2(2);
>
> Foo<false> foo3(foo1);
> Foo<false> foo4(foo2);
>
> Foo<false> foo5(5);
>
> foo5 = foo1;
> foo5 = foo2;
>
> return 0;
> }
[SNIP]
> the compile doesn't recognize my copy constructor
>
> Foo<false>::Foo(const Foo<false>&);
>
> to set up 'foo3', neither my operator=
>
> void Foo<false>::operator= (const Foo<false>&);
>
> to assign foo1 to foo5.
> instead, it uses it's own auto-generated ones.
>
> can you tell me if there is any reason for this?
The standard says so. Template constructors can never be copy constructors
(12.8p3)., and template assignment operators can never be copy assignment
operators (12.8p9).
_Why_ this is so, I don't know.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: Fri, 15 Jun 2001 17:09:43 GMT Raw View
The presence of a template member function that might
expand to a copy constructor or operator= does not
inhibit the generation of the implicit one that the
compiler does anyway. The implicitly generated one
isn't a template, and is hence prefered to any template
specialization.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Fri, 15 Jun 2001 19:23:30 GMT Raw View
In article <slrn9iitmf.647.viisi@elcher.stranitzky.org>, Andi Riedl says...
>
>hi!
>
>after some longer debug session i became aware of some
>ill looking behaviour of gcc (2.95 & 2.97) and would
>like to know if this really is violating the standard.
>the compile doesn't recognize my copy constructor
>
> Foo<false>::Foo(const Foo<false>&);
>
>to set up 'foo3', neither my operator=
>
> void Foo<false>::operator= (const Foo<false>&);
>
>to assign foo1 to foo5.
>
>instead, it uses it's own auto-generated ones.
A template ctor never is a copy constructor - even if it's
signature is the same as that of a copy constructor.
Therefore, your class doesn't have a copy ctor, and the
compiler generates one.
Regards,
Michiel Salters
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]