By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Template specialization with a template

P: n/a
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

Nov 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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)
{
.......
}
Nov 10 '06 #2

P: n/a

Loopy wrote:
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<floatf_matrix;
vfm.Add(f_matrix);

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

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

Nov 10 '06 #3

P: n/a
>
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

Nov 10 '06 #4

P: n/a
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<floatf_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

Nov 10 '06 #5

P: n/a

Loopy wrote:
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<floatf_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
Then modify it to fit the bill, thats trivial.

Nov 10 '06 #6

P: n/a
>
But then vector has to know about matrix - that is not acceptable as a
solutionThen modify it to fit the bill, thats trivial.
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.

Nov 11 '06 #7

P: n/a

Loopy wrote:
But then vector has to know about matrix - that is not acceptable as a
solutionThen modify it to fit the bill, thats trivial.

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.

Nov 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.