473,385 Members | 1,486 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,385 software developers and data experts.

Template specialization with a template

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
7 1446
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

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
>
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
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

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
>
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

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 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...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
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...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
4
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.