Template specialization with a template

L

Loopy

Assume I have (excuse possible unintended syntax errors)

template <class T>
class vector
{
....
void Add(const T &);
.....
}

template <class T>
void vector::Add(const T&t)
{
.......
}


And I have another template class

template <class T>
class matrix
{
....
....
}

I want to specialize vector::Add for all matrix<T>
e.g. something like
void vector< matrix<T> >::Add(const matrix<T> &t)
{
.......
}

At the moment I am writing

template <>
void vector< matrix<float> >::Add(const matrix<float> &t)
{
.......
}

template <>
void vector< matrix<float> >::Add(const matrix<double> &t)
{
.......
}

etc.......
Just can't figure the syntax and searching has not turned up a
solution...

Andrew
 
G

Gianni Mariani

Loopy wrote:
...
At the moment I am writing

template <>
void vector< matrix<float> >::Add(const matrix<float> &t)
{
......
}

try this:

template <typename T1, typename T2>
void vector< matrix<T1> >::Add(const matrix<T2> &t)
{
.......
}
 
S

Salt_Peter

Loopy said:
Assume I have (excuse possible unintended syntax errors)

template <class T>
class vector
{
...
void Add(const T &);
....
}

template <class T>
void vector::Add(const T&t)
{
......
}


And I have another template class

template <class T>
class matrix
{
...
...
}

I want to specialize vector::Add for all matrix<T>
e.g. something like
void vector< matrix<T> >::Add(const matrix<T> &t)
{
......
}

At the moment I am writing

template <>
void vector< matrix<float> >::Add(const matrix<float> &t)
{
......
}

template <>
void vector< matrix<float> >::Add(const matrix<double> &t)
{
......
}

etc.......
Just can't figure the syntax and searching has not turned up a
solution...

Andrew

Note the staggered templates in the inline function's implementation:

#include <iostream>

template< typename T >
class matrix
{
};

template< typename M >
class vector
{
public:
template< typename U >
void Add(const matrix< U >&);
};

template< typename M >
template< typename U >
void vector< M >::Add(const matrix< U >& r_matrix)
{
// do stuff
}

int main()
{
vector< matrix<float> > vfm;

matrix<float> f_matrix;
vfm.Add(f_matrix);

matrix< double > d_matrix;
vfm.Add(d_matrix);

matrix< int > n_matrix;
vfm.Add(n_matrix);
}
 
L

Loopy

try this:

template <typename T1, typename T2>
void vector< matrix<T1> >::Add(const matrix<T2> &t)
{
......
}

I get error : template argument list must match the parameter list
 
L

Loopy

Note the staggered templates in the inline function's implementation:

#include <iostream>

template< typename T >
class matrix
{
};

template< typename M >
class vector
{
public:
template< typename U >
void Add(const matrix< U >&);
};

template< typename M >
template< typename U >
void vector< M >::Add(const matrix< U >& r_matrix)
{
// do stuff
}

int main()
{
vector< matrix<float> > vfm;

matrix<float> f_matrix;
vfm.Add(f_matrix);

matrix< double > d_matrix;
vfm.Add(d_matrix);

matrix< int > n_matrix;
vfm.Add(n_matrix);
}


But then vector has to know about matrix - that is not acceptable as a
solution
 
L

Loopy

Don't get what you are saying. You have added a member function to
vector that takes the type matrix. I can't be adding a member function
to vector for every new type I want to specialize on.
 
S

Salt_Peter

Loopy said:
Don't get what you are saying. You have added a member function to
vector that takes the type matrix. I can't be adding a member function
to vector for every new type I want to specialize on.

If you need to decouple the matrix class, you need a conversion ctor in
the matrix class.
The compiler needs to know what conversions are required so you'll have
to template that matrix convertor. That means you may (or may not) need
accessors to the matrix's private parts.

If i were you, i'ld use a presized std::vector with default initialized
float elements in a Container class.
Add(...) would become Set(const size_t index) or whatever. However, at
this point, thats all hypothetical since too little info is provided.
 

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,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top