Topic: clarification on copy constructor member template specialization
Author: rtw@freenet.co.uk (Rob Williscroft)
Date: Fri, 25 Mar 2005 04:00:38 GMT Raw View
vijay wrote in news:1111403214.858281.325660@f14g2000cwb.googlegroups.com
in comp.std.c++:
> Can anybody tell why first two specializations are illegal, but not the
> third one. I referred 14.7 but it is not clear to me.
>
> template <class TT> struct A {
> template <class T> A(const T&) {}
> };
> template <> template <> A<short>::A(const A<short>&); //illegal
>
The above has the signature of the copy constructor, but you havent
declared that so you can't specialize it. If you had declared it you
would explicitly specialize it like this:
template <> A< short >::A( A const & );
If you add a volatile to the mix then it works:
template <class TT>
struct A
{
A() {}
template <class T> A(const volatile T&) {}
};
template <> template <> A<short>::A( A<short> const volatile & );
As you are no longer redefining *the* copy constructor.
> template <> template <> A<short>::A(const A&); //illegal
>
Again the copy constuctor, but 1) no declaration and 2) there is
that extra "template <>".
Rob.
--
http://www.victim-prime.dsl.pipex.com/
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: Alberto Barbati <AlbertoBarbati@libero.it>
Date: Thu, 24 Mar 2005 23:15:20 CST Raw View
vijay wrote:
> Can anybody tell why first two specializations are illegal, but not the
> third one. I referred 14.7 but it is not clear to me.
>
> template <class TT> struct A {
> template <class T> A(const T&) {}
> };
> template <> template <> A<short>::A(const A<short>&); //illegal
>
> template <> template <> A<short>::A(const A&); //illegal
>
Both:
template <> A<short>::A(const A&)
and
template <> A<short>::A(const A<short>&)
refer to the copy constructor of class A<short>, which has been
implicitly declared. As it is a regular member function and not a member
template, it cannot be explicitly specialized, thus the error.
In order to let the compiler understand that you really mean to
specialize your member template and not the regular function, you have
to use this syntax instead:
template <> template <> A<short>::A<>(const A<short>&);
^^
template <> template <> A<short>::A<>(const A&);
^^
In case of A<long> there is no ambiguity and the compiler is able to
understand on its own about your intent.
HTH,
Alberto
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: "vijay" <vijaypcin@gmail.com>
Date: Tue, 22 Mar 2005 14:40:30 CST Raw View
Can anybody tell why first two specializations are illegal, but not the
third one. I referred 14.7 but it is not clear to me.
template <class TT> struct A {
template <class T> A(const T&) {}
};
template <> template <> A<short>::A(const A<short>&); //illegal
template <> template <> A<short>::A(const A&); //illegal
template <> template <> A<short>::A(const A<long>&); //legal
- vijay
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]