|
-
April 4th, 2010, 03:02 AM
#1
Best practice to implement this template class
I have a class which currently contains N vectors of int
and M vectors of double like below;
Code:
template< int N, int M >
class VectorCollection {
std::vector<int> ivec[N];
std::vector<double> dvec[M];
};
probably in future vectors of other types (e.g. bool) will be added
to class. I want to have a simple interface which give access to
interior vectors based on the types of int,double,...
like below
Code:
template< int N, int M >
class VectorCollection {
// vectors as above
public:
template< class T, int K >
T& data(i);
};
data<int,0>(i) should return ivec[0][i];
data<double,1>(i) should return dvec[1][i] and so
But I do not know how to implement this. It seems very difficult.
Could anyone help me on this?
Thanks a lot
-
April 4th, 2010, 03:59 AM
#2
Re: Best practice to implement this template class
well, you can't do a template partial specialization on a member function, but the question is why would you want to do so? why can't you make two data functions - one for vector of doubles and the other for vectors of integers? that's simpler and there is no code bloat.
-
April 4th, 2010, 06:27 AM
#3
Re: Best practice to implement this template class
Regarding to my needs I am sure that the above interface is good for me.
And it is hard for me ( and maybe boring for you ) to tell why I need that
I need just that to work, is there any good solution?
Thanks
-
April 4th, 2010, 06:46 AM
#4
Re: Best practice to implement this template class
 Originally Posted by ar115
Regarding to my needs I am sure that the above interface is good for me.
And it is hard for me ( and maybe boring for you ) to tell why I need that
I need just that to work, is there any good solution?
Thanks
It's not so difficult to implement, but I think you should consider redesign your application because this can easily lead to code bloat, since for every different combination of <T, K> you actually create a new function(that is the way templates work as far as I know)!
anyway, if that's really what you want you can use function overloading passing a dummy paramter:
Code:
template< int N, int M >
class VectorCollection {
private:
std::vector<int> ivec[N];
std::vector<double> dvec[M];
public:
double& data(int i, int k, double dummy)
{
return dvec[k][i];
}
int& data(int i, int k, int dummy)
{
return ivec[k][i];
}
template<class T, int K>
T& data(int i)
{
return data(i, K, T());
}
};
Last edited by Regel; April 4th, 2010 at 06:49 AM.
-
April 4th, 2010, 07:39 AM
#5
Re: Best practice to implement this template class
Many Thanks! it is a good solution
But is there better ones?
I was wondering if we can use a nested class and delegate things to it. since we can
use partial specialization for classes, something like this
Code:
template< int N, int M >
class VectorCollection {
std::vector<int> ivec[N];
std::vector<double> dvec[M];
template< class T, int N >
struct nested{
// implementation stuff
};
public:
template< class T, int N >
T& data( int i ){
return nested<T,n>::getdata(i);
}
};
Then the question is that what the "// implementation stuff" of struct nested must be?
Thanks
-
April 4th, 2010, 09:52 AM
#6
Re: Best practice to implement this template class
 Originally Posted by ar115
Many Thanks! it is a good solution
But is there better ones?
I was wondering if we can use a nested class and delegate things to it. since we can
use partial specialization for classes, something like this
Code:
template< int N, int M >
class VectorCollection {
std::vector<int> ivec[N];
std::vector<double> dvec[M];
template< class T, int N >
struct nested{
// implementation stuff
};
public:
template< class T, int N >
T& data( int i ){
return nested<T,n>::getdata(i);
}
};
Then the question is that what the "// implementation stuff" of struct nested must be?
Thanks
Yes you can do this:
Code:
template< class T, int N >
struct nested{ // that's for double
T& get() { .. }
};
template<int N > // thats for int
struct nested<int, N>{
int& get() { .. }
};
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|