Topic: Explicit member template specializations.
Author: "BigMan" <BigMan@abv.bg>
Date: Fri, 24 Jun 2005 09:00:07 CST Raw View
Can anyone point out a reason why an explicit specialization of a
template member cannot be a member of a non-explicit specialization:
template< int i >
struct s1
{
template< int j >
struct s2 { };
};
template< int i >
template< >
struct s1< i >::s2< 1 > { };
---
[ 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: "Alipha" <aliphax@hotmail.com>
Date: Sat, 25 Jun 2005 20:05:12 CST Raw View
afaik, the only way to do that is:
template< int i >
struct s1
{
template< int j >
struct s2 { };
template<>
struct s2<1> { };
};
of course, i'm assuming you want to do that outside of the struct s1.
i don't have a good solution for you. perhaps you could have a struct
inside the struct s1 that inherits from a struct outside the struct:
template< int j > // perhaps put this in namespace details?
struct s2 { };
template< int i >
struct s1
{
template< int j >
struct s2 : public ::s2<j> { };
};
template<>
struct s2< 1 > { };
---
[ 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: "BigMan" <BigMan@abv.bg>
Date: 26 Jun 2005 06:50:12 GMT Raw View
Your suggestion:
template< int i >
struct s1
{
template< int j >
struct s2 { };
template<>
struct s2<1> { };
};
is not a solution, although some compilers accept it. It just violates
the standard.
---
[ 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: gp.kiwi@gmail.com (Graeme Prentice)
Date: Sun, 26 Jun 2005 15:09:29 GMT Raw View
On Fri, 24 Jun 2005 09:00:07 CST, BigMan wrote:
>Can anyone point out a reason why an explicit specialization of a
>template member cannot be a member of a non-explicit specialization:
>
>template< int i >
>struct s1
>{
> template< int j >
> struct s2 { };
>};
>
>template< int i >
>template< >
>struct s1< i >::s2< 1 > { };
>
For the case of nested class templates (as opposed to functions) I can't
think of any particular reason, given that a nested class template can
be partially specialized, however if you read through all of 14.7.3
(explicit specialization) you get the feeling there's a reasonable
amount of complexity involved, so maybe it was because of not wanting to
overload an already complex environment with more syntax or wasn't
understood well enough back in the nineties.
In 14.5.4.3/2 you can see a primary template of a nested class described
as an "explicit specialization" even though it's actually a primary
template in itself, as well as being an explicit specialization of a
sort - I think there's a terminology glitch there.
A workaround for your particular example is this
#include <iostream>
template< int i >
struct s1
{
template< int j, typename T = int >
struct s2 { };
};
template< int i >
template<typename T>
struct s1< i >::s2<1,T> { static const int v1 = 16+i;};
int main()
{
s1<3>::s2<1> a1;
std::cout << a1.v1;
s1<4>::s2<1> a2;
std::cout << a2.v1;
}
Graeme
---
[ 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 ]