Re: Variadic templates
Howard Gardner wrote:
Greg Herlihy wrote:
Howard Gardner wrote:
These are the particular stumbling blocks that hurt the worst, and I run
into them fairly often (or, more correctly, I deploy workarounds for
them fairly often).
1) If you write:
template< typename T, T V > struct sometype;
then there's no way to partially specialize it for a particular type. It
seems to me that this should work (but of course it doesn't).
template< int V > struct sometype< int, V >;
.because the partial specialization of "sometype" does not declare a
complete type. A full or partial specialization of a class template
needs to be a complete type in order to instantiate objects with it:
template< int V >
struct sometype< int, V > {};
Now the partial specialization of sometype works. For example:
sometype< int, 7> mytype;
will use the partial specialization above.
#include <ostream>
#include <cstddef>
using namespace std;
template< typename T, T V >
struct sometype{static const int value = 0;};
template< int V >
struct sometype< int, V > {static const int value = 1;};
int main()
{
cout << sometype< int, 4 >::value << endl;
}
Using comeau, this prints "0". It instantiated the primary template, not
the specialization.
..which is wrong. A matching specialization is always considered a
better match than the general class template. And given the fact that
there are no other specializations of "sometype" from which to choose,
there can be no doubt that the value of sometype<int, 4>::value has to
be "1".
I would suggest reporting this bug to the compiler maker. You may also
wish to consider switching to another C++ compiler that does not have
this defect. I can confirm, for example, that gcc 4.01 selects the
sometype specialization correctly.
Greg
---
[ 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.comeaucomputing.com/csc/faq.html ]