Topic: Partial specializations
Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/03/07 Raw View
Looks like a bug in your compiler.
You may want to use:
template <typename TYPE>
class Foo<TYPE, 0u>
as it looks marginally more elegant.
Andrei
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fvali@biotrack.com
Date: 1999/03/08 Raw View
In article <36E0FFB2.EFAA7DAB@home.com>,
Jody Hagins <jodyh@home.com> wrote:
<snip>
> Ironically, it
> also works if I change the "unsigned int" to a "char *" so it is
> allowing 0 to be an int and a char *, but not an unsigned int.
I believe another bug in your compiler is that it allows 0 to be a valid
template argument for a non-type template parameter which is not of integral
type.
So if we use 0 as an actual template argument in the following case:
template<char *cp> struct C { };
C<0> c; // ill-formed, pointer conversions are not applied
Therefore I believe that your partial specialization should not compile at all
if your non-type parameter is a pointer and your partial specialization
specializes on the non-type parameter using a null-pointer constant.
so which compiler is it?
-fais
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1999/03/09 Raw View
In article <36E0FFB2.EFAA7DAB@home.com>,
Jody Hagins <jodyh@home.com> wrote:
> Should a comforming compiler recognize the following partial
> specialization as terminating the recursive template definition?
>
> template <class TYPE, unsigned int N>
> class Foo
> {
> Foo<TYPE, N-1> zz_;
> };
>
> template <class TYPE>
> class Foo<TYPE, 0>
> {
> TYPE t;
> };
>
> The compiler keeps going until it blows past the recusrion level limit.
> It never stops when N becomes 0.
I believe that this is supposed to work.
> However, if N is changed from
> "unsigned int" to "int" then it works like it should.
Interesting. Perhaps it is failing to coerce the 0 into an unsigned
int. You don't say which brand "The compiler" is, but you might have
a workaround if you use
template <class TYPE> class Foo<TYPE, 0U> /* ... */
instead. Or, this might not work -- but it's free to try it, right?
> Ironically, it
> also works if I change the "unsigned int" to a "char *" so it is
> allowing 0 to be an int and a char *, but not an unsigned int.
I don't see why this should ever work. If you start with a valid
char* and keep subtracting 1, does it ever reach the NULL value?
On most architectures that I'm familiar with, the value will be a
relocatable value, with no known absolute value until link time or
later. Subtracting one from this value repeatedly should never get
the NULL value, should it?
On the other hand, if the address of N's string is known to be
(say) 0x1234, then it should take 0x1234 levels of nesting before
the value looks like NULL. I wouldn't expect this to work anywhere
either.
In short, if this happens to work it is a VERY strange fluke.
> Am I wrong for expecting the compiler to recognize the partial
> specialization?
If the compiler is billed as 100% ANSI or ISO compliant or
conforming, then you seem to have found a bug. However, most
compilers aren't 100% compliant yet. Perhaps you are being
premature. Check the documentation that came with the compiler.
----
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jody Hagins <jodyh@home.com>
Date: 1999/03/06 Raw View
Should a comforming compiler recognize the following partial
specialization as terminating the recursive template definition?
template <class TYPE, unsigned int N>
class Foo
{
Foo<TYPE, N-1> zz_;
};
template <class TYPE>
class Foo<TYPE, 0>
{
TYPE t;
};
The compiler keeps going until it blows past the recusrion level limit.
It never stops when N becomes 0. However, if N is changed from
"unsigned int" to "int" then it works like it should. Ironically, it
also works if I change the "unsigned int" to a "char *" so it is
allowing 0 to be an int and a char *, but not an unsigned int.
Am I wrong for expecting the compiler to recognize the partial
specialization?
Thanks.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jody Hagins <jodyh@home.com>
Date: 1999/03/06 Raw View
Jody Hagins wrote:
> template <class TYPE>
> class Foo<TYPE, 0>
> {
> TYPE t;
> };
Oh yeah.. I forgot to mention one other thing... If I do
class Foo<TYPE, (size_t)0>
{
TYPE t;
};
then it works as you would expect...
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/03/06 Raw View
On 06 Mar 99 12:23:20 GMT, Jody Hagins <jodyh@home.com> wrote:
>Should a comforming compiler recognize the following partial
>specialization as terminating the recursive template definition?
>
>template <class TYPE, unsigned int N>
>class Foo
>{
> Foo<TYPE, N-1> zz_;
>};
>
>template <class TYPE>
>class Foo<TYPE, 0>
>{
> TYPE t;
>};
Yes.
(I normally use 'size_t' instead of 'unsigned'.)
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]