partial template specializations w/ bcc

J

John Doe

I am using the free bcc compiler and was playing with partial ts:

below works ok:

template<unsigned M=3, unsigned N=3, typename T=double>
class Matrix
{
public:
Matrix() { _data=new T[M*N]; }
~Matrix() { delete [] _data; }

T& operator() (unsigned row, unsigned col)
{ return _data[row*M+col]; }
T operator() (unsigned row, unsigned col) const
{ return _data[row*M+col]; }
private:
T* _data;
};

template<unsigned M, unsigned N, typename T>
class Matrix<3,3,T>
{
public:
Matrix() { _data=new T[9]; }
~Matrix() { delete [] _data; }

T& operator() (unsigned row, unsigned col)
{ return _data[row<<1+col]; }
T operator() (unsigned row, unsigned col) const
{ return _data[row<<1+col]; }
private:
T* _data;
};

but if I try to put member definitions into another file say matrix.cpp
like this

template<unsigned M, unsigned N, typename T>
Matrix<3,3,T>::Matrix() {}

it just doesnt work + the compiler sometimes decides a function should be
inline when I don't want it to be and linking with the .obj containing
inlined functions does not work.
 
J

John Harrison

John said:
I am using the free bcc compiler and was playing with partial ts:

below works ok:

template<unsigned M=3, unsigned N=3, typename T=double>
class Matrix
{
public:
Matrix() { _data=new T[M*N]; }
~Matrix() { delete [] _data; }

T& operator() (unsigned row, unsigned col)
{ return _data[row*M+col]; }
T operator() (unsigned row, unsigned col) const
{ return _data[row*M+col]; }
private:
T* _data;
};

template<unsigned M, unsigned N, typename T>
class Matrix<3,3,T>

This is wrong, should be

template<typename T>
class Matrix<3,3,T>

You've specialised M and N, so they shouldn't be named as template
parameters.
{
public:
Matrix() { _data=new T[9]; }
~Matrix() { delete [] _data; }

T& operator() (unsigned row, unsigned col)
{ return _data[row<<1+col]; }
T operator() (unsigned row, unsigned col) const
{ return _data[row<<1+col]; }
private:
T* _data;
};

but if I try to put member definitions into another file say matrix.cpp
like this

template<unsigned M, unsigned N, typename T>
Matrix<3,3,T>::Matrix() {}

Again

template<typename T>
Matrix said:
it just doesnt work + the compiler sometimes decides a function should
be inline when I don't want it to be and linking with the .obj
containing inlined functions does not work.

Hmmm, how can you have got this far with templates without realising
that you never, ever put template code in a .cpp file?

Put all your template code in the header file. You can have your
functions inline or not inline, it doesn't matter, just put all the code
in the header file. That is how templates work.

john
 
J

John Doe

This is wrong, should be

template<typename T>
class Matrix<3,3,T>

Yes, I've tried this aswell, but it does not work. It seems the free bcc
compiler just doesnt support template specialization properly?

Anyway, why am I using template classes at all? I wish to provide a
special case for the Matrix<3,3,T> type where I can do
_data[(row<<1)+3+col] when accessing matrix elements (thereby dropping the
multiplication) as well as other special handling. Are there other ways to
specialize a type?
 
S

Shezan Baig

John said:
This is wrong, should be

template<typename T>
class Matrix<3,3,T>

Yes, I've tried this aswell, but it does not work. It seems the free bcc
compiler just doesnt support template specialization properly?

Anyway, why am I using template classes at all? I wish to provide a
special case for the Matrix<3,3,T> type where I can do
_data[(row<<1)+3+col] when accessing matrix elements (thereby dropping the


I don't think this is worth it. Sounds like premature optimization to
me. Also, you probably don't want to allocate and deallocate the T
array elements in the ctor and dtor. Since you already know the size
at compile-time, you can just use a regular array. That way, you can
drop the constructor and destructor, and you don't need to define a
copy constructor and assignment operator (which I noticed you didn't
have anyway in the original code). There is a lot of extra code in
your class that doesn't need to be there.

multiplication) as well as other special handling. Are there other ways to
specialize a type?


Nope. If the compiler doesn't support it, you're out of luck... Try
downloading g++.

-shez-
 
J

John Harrison

John said:
Yes, I've tried this aswell, but it does not work. It seems the free
bcc compiler just doesnt support template specialization properly?

That's possible, I'm not familar with that compiler.

The free g++ compiler does support partial template specialisation
however, maybe you could switch to that?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top