This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Full specialization


garloff@kg1:/home/garloff/C > egcc -O2 -c bug_fullspec.cc
bug_fullspec.cc: In method Tensor<2>::Tensor(int, int)':
bug_fullspec.cc:29: Internal compiler error.
bug_fullspec.cc:29: Please submit a full bug report to egcs-bugs@cygnus.com'.
garloff@kg1:/home/garloff/C > egcc -v
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/egcs-2.91.14/specs
gcc version egcs-2.91.14 980315 (gcc-2.8.0 release)

/* bug_fullspec.cc */
/* egcs (980315) bug */
/* 98/03/16 Kurt Garloff <K.Garloff@ping.de> */

#include <stdio.h>

template <unsigned rank>
class Tensor
{
 protected:
	int dim[rank];
	double *data;
	
 public:
	Tensor (const int d = 0)
	{       
		int ne = 1; 
		for (int i = 0; i < d; i++)
		{ ne *= d; dim[i] = d; };
		data = new double[ne];
	};
};

template <unsigned rank>
class Tensor<2> : Tensor<rank>
{
 public:
	Tensor<2> (const int d0, const int d1)
	{
		dim[0] = d0; dim[1] = d1;
		data = new double[d0*d1];
	};
};

int main ()
{
	Tensor<4> t4 (3);
	Tensor<2> t2 (2, 3);
};


Well, yes, I think the specialization syntax I used here is wrong. No excuse
to throw a ,,internal compiler error``, however.

Now for the solution of this (C++ experts, please help!)

Changing it to 
template <>
class Tensor<2> : Tensor<2>

however leads to
garloff@kg1:/home/garloff/C > egcc -O2 -c bug_fullspec.cc
bug_fullspec.cc:26: base class Tensor<2>' has incomplete type
bug_fullspec.cc: In method Tensor<2>::Tensor(int, int)':
bug_fullspec.cc:30: dim' undeclared (first use this function)
bug_fullspec.cc:30: (Each undeclared identifier is reported only once
bug_fullspec.cc:30: for each function it appears in.)
bug_fullspec.cc:31: data' undeclared (first use this function)

I guess, C++ doesn't let me do it this way.

Another way would be to have a decl with Tensor<rank> and only a specialized
definition, i.e.

template <unsigned rank>
class Tensor
{
     /* ... */
     Tensor (const int, const int);
};

template <>
Tensor<2>::Tensor (const int d0, const int d1)
{
 /* ... */
};

This has the bid disadvantage, that errors (Tensor<3> (int, int) will 
only be discovered in the linking stage and not by the compiler.

Any solutions ?
     
-- 
Kurt Garloff, Dortmund 
<K.Garloff@ping.de>
PGP key on http://student.physik.uni-dortmund.de/homepages/garloff


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]