Hi all,
This is my situation:
I've made a template class my_vector that works like normal vectors. I have a normal random access iterator and a random access that returns sorted elements(
sortIterator)
template<typename T, typename F> class my_vector{...}
where T is the type of elements to be stored in my_vector, and F is a
functor that determines the sorting strategy, for example ascending or descending order.
Now i need a global template funtion that checks if two my_vector objects, contains the same values, in the same amount, no matter the order.
For example:
[2,1,4,5,4,2] and [5,4,2,1,2,4] passed to this funtion will return true.
If i have two my_vectors containing different types of value, as
my_vector<char,whatever1> and my_vector<double, whatever2> the function returns false.
My idea:
- if 2 my_vector are on the same type and on the same functor (and the same size), simply call sortIterator on both and cicle them, confronting the value at each step.
- if they are on same type but different functors, create a new my_vector copying data from one and the functor from the other, so now i am in the previous condition.
- if they are on two different types, function returs false.
My first thought was: well what's the problem i'll write 3(4) different template function, one for each case and problem is solved:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//same type, same functor
template<typename T, typename F> bool check(const my_vector<T, F>& vec1,const my_vector<T, F>& vec2){
bool same=false;
if(vec1.size()==vec2.size(){
my_vector<T,F>::sortIterator begin1,begin2,end1,end2;
begin1=vec1.begin();
beign2=vec2.begin();
end1=vec1.end();
end2=vec2.end();
while(begin1!=end1 && begin2!=end2){
if(*begin1==*begin2){
same=true;
begin1++;
begin2++;
else{
same=false;
begin1=end1; //to end the while
}
}
}
return same;
}
|
1 2 3 4 5 6 7 8
|
//same type different functor
template<typename T, typename F, typename F1> bool check(const my_vector<T, F>& vec1,const my_vector<T, F1>& vec2){
if(vec1.size()==vec2.size(){
my_vector<T, F> vec3=vec2;
return check(vec1,vec3);
}
else return false;
}
|
1 2 3
|
//different type, same functor
template<typename T, typename T1, typename F> bool check(const my_vector<T, F>& vec1,const my_vector<T1, F>& vec2){
return false;}
|
1 2 3
|
//different type, different functor
template<typename T, typename T1, typename F, typename F1> bool check(const my_vector<T, F>& vec1,const my_vector<T1, F1>& vec2){
return false;}
|
Now i can confront every my_vector, being sure each case is covered.
But is this a good solution?
Is there a way to put everything in a single function?
I've tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
template<typename T, typename T1, typename F, typename F1> bool check(const my_vector<T, F>& vec1,const my_vector<T1, F1>& vec2){
bool same=false;
if(typeid(T1)==typeid(T2)){
if(vec1.size()==vec2.size()){
my_vector<T1, F1> vec3=vec2; //this causes me problems compiling
//***problem in detail after code***
my_vector<T1,F1>::sortedIterator begin1, end1, begin2, end2;
begin1 = vec1.begin();
begin2 = vec3.begin();
end1 = vec1.end();
end2 = vec3.end();
while(begin1!=end1 && begin2!=end2){
if(*begin1==*begin2){
same=true;
begin1++;
begin2++;
}
else{
same=false;
begin1=end1;
}
}
}
return same;
}
|
*** the main problem ***
Since i can create a my_vector of everything, not only int, char etc.. but also struct,array and whatever(in theory) i can't get the code to compile cause of this instruction
my_vector<T1, F1> vec3=vec2
, that is a call to conversion-constructor.
For example if i have:
1 2 3 4
|
my_vector<my_struct, my_struct_functor> vec1;
my_vector<another_random_data_type, proper_functor> vec2;
check(vec1, vec2)
|
it won't compile
Is there a solution?
What if i use the first method, 4 template functions?
Could anyone come to me and say "You can't do that, because you're violating this or that rule..."?
Can I create many version of the same function?
Isn't a template function supposed to cover every possible case?
Thanks