MESSAGE
DATE | 2011-05-26 |
FROM | Ruben Safir
|
SUBJECT | Subject: [ruben@mrbrklyn.com: Re: [NYLXS - HANGOUT] C++ Workshop - New
|
----- Forwarded message from Ruben Safir -----
Date: Wed, 25 May 2011 17:10:40 -0400 From: Ruben Safir To: Ruben Safir Cc: nylug-talk-at-nylug.org, mrbrklyn-at-panix.com Subject: Re: [NYLXS - HANGOUT] C++ Workshop - New Webservices with OKWS - Tuesday Night User-Agent: Mutt/1.5.20 (2009-06-14)
n Monday 23 May 2011 16:37:36 Ruben Safir wrote: > 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)
... > 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
< Out of curiosity, is this typcastable? e.g.:
dump->find_value((stats::Distribution)search_val);>
That is exactly the type that the function takes in the context of the calling, class. T is an int. The issue winds up being that of implicit conversion which happens if there is a stats::Distirbution single argument constructor. For this to happen, the Template argument needs to be specifically defined, which it is in this case since it was defined in the class.
<< [function for reference: void List::find_value(T val) ]
I think this is a case where the compiler needs help, because find_value() is looking for a templated generic value, so the compiler doesn't know that it can't send in a simple integer. The whole purpose of using templates is to keep the type abstract, so I'm not sure how you'd solve this within the function header. >>
As it turns out, this is an explicit rule. If T is not defined, the implicit conversion can not happen a`nd it is considered, "Can not be deduced".
o
<< I've been trying to read your C++ code, but I've been finding it difficult; the stream of templated objects... wow. However in the middle of trying to read things that I can parse, I noticed the following: >>
Well, it's an excersize in templates. Your familiar with the rules for vector<> and list<> because they are part of the standard template libray. My chainlist::List seems to be pretty decent at this point, but the syntax is unknown unless you pour over the code *List<>->cursor() returns a chainlist::Node class object. It contains a pointer to a value of type T and a pointer to the Next Node
Things get exciting when T is its own class.
<< float mean_list( ) ... while(tallies->cursor() != tallies->endd() ){ ... if(dump->cursor() != NULL) ... } ... if(dump->cursor() != 0)
If dump->cursor() is a pointer, then compare it with NULL, not 0. >> There is a lot of debate on this issue. Strausoup strongly urges 0, but frankly, dealing with nulls in C++ is a PIA, and even moreso now with the new standard which blows a warning at you if you assign NULL to an int type variable.
<< Even if this works, I'd choose one test and try to stick to it for readability reasons. >>
Ruben
-- Chris
-- Chris Knadle Chris.Knadle at coredump.us
-- 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 -----
|
|