473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

partial template specializations w/ bcc

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.
Sep 10 '05 #1
4 1033
John Doe wrote:
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<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.


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
Sep 10 '05 #2
On Sun, 11 Sep 2005 01:39:14 +0200, John Harrison
<jo*************@hotmail.com> wrote:
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?
Sep 11 '05 #3

John Doe wrote:
On Sun, 11 Sep 2005 01:39:14 +0200, John Harrison
<jo*************@hotmail.com> wrote:
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-

Sep 11 '05 #4
John Doe wrote:
On Sun, 11 Sep 2005 01:39:14 +0200, John Harrison
<jo*************@hotmail.com> wrote:
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?


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
Sep 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
1
by: Samee Zahur | last post by:
Why aren't we allowed to do partial specializations like these on numeric template parameters? That would have allowed us to do all kinds of interesting stuffs like loops of variable nesting (bad...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
1
by: Martin | last post by:
I have a couple of partial specializations of a templated class, but my code results in "undefined reference" errors by the linker. Can't a partial specialization reuse member functions of the...
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.