template specialisation

D Maley d.maley@airforce1.stmarys-belfast.ac.uk
Mon Nov 9 05:43:00 GMT 1998


1. Could you please tell me what I can expect from template specialisation,
and what is "uncompilable". I'm not sure whether what I'm seeing is an
oversight or whether I'm asking for things that can't be done. I'm on a
P200, Solaris 2.6, egcs 1.1b.

I have a class

template <class T> Manager;

for which I wish to write specialisations for most STL containers, i.e.

template <class T> Manager<vector<T> > { /* .. */ };

template <class T> Manager<set<T> > { /* .. */ };

My problem comes when I am faced with a type taking two significant
parameters e.g. pair<S, T>.

I would like to write a specialisation

template <class T> Manager<pair<typename T::first_type, typename
T::second_type> > { /* .. */ };

but this creates unfathomable errors from the compiler. The most
understandable has been "variable x has initialiser but incomplete type" if
I try to declare x to be, say,

Manager<pair<vector<int>, vector<int> > > x();

I read in Stroustrup that the actual type parameter in a specialisation is
deduced from the specialisation pattern; is the construct 'pair<typename
T::first_type, typename T::second_type>' requiring too much deduction?

2. What should happen to default template parameters within specialisations?
They seem to be accepted by the compiler, but then not used. For example, in
the regular template class Manager below, I can declare a Manager by giving
only one parameter. However, in the template class SManager defined by
specialisation, if I give only one parameter, compilation is unsuccessful.

#include <vector.h>

template <class T1, class T2=T1> class Manager {};
template <class T1, class T2> class SManager;
template <class T1, class T2=T1> class SManager<vector<T1>, T2> {};

void main(int argc, char *argv[])
{
  Manager<vector<int> > m;
  SManager<vector<int> > sm;
}

gcc -DXTFUNCPROTO -DSUN -w -falt-external-templates -I/usr/openwin/include
-I/usr/dt/share/include -I.  -c -o test.o test.cpp 2>>err
*** Error code 1
make: Fatal error: Command failed for target `test.o'
test.cpp: In function `int main(...)':
test.cpp:10: incorrect number of parameters (1, should be 2)
test.cpp:5: in template expansion for decl `template <class T1, class T2>
SManager<T1,T2>'

        --- |================================================|
      ----  |-=|         Dave Maley, M.A., M.Phil.        |=-| ,---I
      ---   |================================================| |== U-----
        --- | Computer Manager, St. Mary's College, Belfast  | |== U (: |
      ---   |________________________________________________| |   U----'-----
     -----  |________________________________/   ____====______|   U'  ()___(|
        ---  #__ //,-,  ,-,  ,-,\\         \|   ///,-,  ,-, \\ -------/,-,\_[
                ''( 0 )( 0 )( 0 )'          o  -''( 0 )( 0 ) \\#####====( 0 )
     --------------'-'--'-'--'-'-------------------'-'--'-'--------------'-'----





More information about the Gcc-bugs mailing list