MESSAGE
DATE | 2011-05-24 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] [ruben@mrbrklyn.com: Re: [mrbrklyn@panix.com: Template Argument
|
----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 12:40:26 -0400 From: Ruben Safir To: Billy Cc: Ruben Safir Subject: Re: [mrbrklyn-at-panix.com: Template Argument type deterination] User-Agent: Mutt/1.5.20 (2009-06-14)
On Tue, May 24, 2011 at 12:25:09PM -0400, Billy Donahue wrote: > You didn't provide the constructor of stats::Distribution. If a constructor > takes a single argument, it will be considered a conversion operator unless > it is marked 'explicit'. Most style guides including Google's urge > programmers to mark any single argument constructor as 'explicit', which > make it ineligible as a conversion operator. > > Another thing is that maybe an integer is being used as a pointer or > something. > I can't really tell exactly through the levels of indirection. > > I'm uneasy because I don't see a single-arg constructor in the 'gdb' ptype > output. > But that isn't source code, that's gdb output. Only source code matters. > The constructor might have been inlined and optimized away, for example. > > On Mon, May 23, 2011 at 4:50 PM, Ruben Safir wrote: >
Ah - thank you for answering. It's driving me crazy and I'm not finding help in texts or on the net.
So now I have a new concept, a conversion operator. What is a conversion operator.
I tried to make this a simpler example, but it is really just related, perhaps not exactly the same.
testm(intval) fails to compile in the below example, as I'd expect since the param type is stats::Distribution and not an int. But testme(intval) does compile with the same kind of promotion
http://www.nylxs.com/docs/workshops/test_del3.cpp.html
http://www.nylxs.com/docs/workshops/test_del3.tgz
There is a difference in this example though because the original behavior was demonstated not by a templated function, but a member of a templated class.
And I agree the indirection starts to make it very difficult to figure out or predict that typename T is being sunstituted for as the template class stacks get deeper.
Ruben > > ----- Forwarded message from Ruben Safir ----- > > > > Date: Mon, 23 May 2011 16:35:54 -0400 > > From: Ruben Safir > > To: hangout-at-nylxs.com > > Subject: Template Argument type deterination > > User-Agent: Mutt/1.5.20 (2009-06-14) > > > > > > 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? > > > > > > ----- End forwarded message ----- > > > > -- > > http://www.mrbrklyn.com - Interesting Stuff > > http://www.nylxs.com - Leadership Development in Free Software > > > > So many immigrant groups have swept through our town that Brooklyn, like > > Atlantis, reaches mythological proportions in the mind of the world - RI > > Safir 1998 > > > > http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI > > Safir 2002 > > > > "Yeah - I write Free Software...so SUE ME" > > > > "The tremendous problem we face is that we are becoming sharecroppers to > > our own cultural heritage -- we need the ability to participate in our own > > society." > > > > "> I'm an engineer. I choose the best tool for the job, politics be > > damned.< > > You must be a stupid engineer then, because politcs and technology have > > been attached at the hip since the 1st dynasty in Ancient Egypt. I guess > > you missed that one." > > > > ? Copyright for the Digital Millennium > > > > > > -- > ?n??uop ?ll?q
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
----- End forwarded message -----
|
|