MESSAGE
DATE | 2011-05-23 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] Template Argument type deterination
|
I'm having trouble understanding why my program is making a change in the argument type of my template class function
template < class T > void List::find_value(T val) { cursor() = front(); while(cursor() != endd()){ if (*(cursor()->value()) == val) return; else{ cursor(cursor()->next()); } } if(*(endd()->value()) == val) return; else{ cursor() = 0; //park the cursor when nothing is found } }
It's part of a List template class
I call it from a templated fucntion in a different namespace as follows:
tally = new chainlist::List >; tallies = new chainlist::List< chainlist::List< stats::Distribution > *>;
std::cout << "The standard mean for picking 7 is ==> " << stats::mean_list(tallies, 7 ) << std::endl;;
template float mean_list(chainlist::List< chainlist::List >* > * tallies, T search_val){ if(tallies->endd() == 0){ std::cout << "Empty List" << std::endl; return 0.0; } int sum = 0; chainlist::List > * dump; tallies->cursor() = tallies->front(); while(tallies->cursor() != tallies->endd() ){ //tallies->cursor()->value()->find_value(search_val); dump = *(tallies->cursor()->value()); //dump->cursor() = dump->endd(); //std::cout << "Testing\n" << //*(dump->cursor()->value()) << std::endl; dump->find_value(search_val); if(dump->cursor() != NULL) sum += dump->cursor()->value()->population(); tallies->cursor(tallies->cursor()->next()); } dump = *(tallies->cursor()->value()); dump->find_value(search_val); if(dump->cursor() != 0) sum += dump->cursor()->value()->population(); float tot = (float) sum/(float)(tallies->size()); std::cout << "Mean " << tot << " sum " << sum << " size " << tallies->size() << std::endl; return tot;
}
Now T is an integer for mean_list, and the first argument is of type
chainlist::List< chainlist::List >* > * when called and the second argument is of type int
I then pass int search_val to method chainlist::List::find_value(T) where T = stats::Distribution (where T is an int) on this line:
dump->find_value(search_val);
and that's where the weirdness happens. I have a type mismatch since I'm sending by value an integer instead of a stats::Distribution
Instead the program calls the stat::Distribution constructor using 7, the value of search_val, as an argument redrawing search_val as type to a Distribution
:in gdb
312 void List::find_value(T val) 313 { 314 cursor() = front(); 315 while(cursor() != endd()){ 316 if (*(cursor()->value()) == val) 317 return; 318 else{ 319 cursor(cursor()->next()); 320 } 321 } 322 if(*(endd()->value()) == val) 323 return; 324 else{ 325 cursor() = 0; //park the cursor when nothing is found 326 } 327 }
chainlist::List >::find_value (this=0x804f008, val=...) at linklist.h:314 (gdb) ptype val type = class stats::Distribution { public: chainlist::List > *tally; private: int freq; int occurances;
public: void Distribution(int, int); void Distribution(void); int description(void) const; int population(void) const; void increase_occ(void); void descrease_occ(void); int operator()(void); bool operator==(stats::Distribution &); bool operator<(stats::Distribution &); float stddev(chainlist::List > *); }
What rule for Template argument matching allows for this kind of magic?
|
|