 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
vijay Guest
|
Posted: Tue Mar 22, 2005 8:40 pm Post subject: clarification on copy constructor member template specializa |
|
|
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 ]
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Fri Mar 25, 2005 4:00 am Post subject: Re: clarification on copy constructor member template specia |
|
|
vijay wrote in news:1111403214.858281.325660 (AT) f14g2000cwb (DOT) googlegroups.com
in comp.std.c++:
| Quote: | 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.
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Fri Mar 25, 2005 5:15 am Post subject: Re: clarification on copy constructor member template specia |
|
|
vijay wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|