MESSAGE
DATE | 2011-05-24 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] [mrbrklyn@panix.com: (fwd) Template argument determination]
|
----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:34 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!not-for-mail From: Ruben Safir Newsgroups: comp.lang.c++ Subject: Template argument determination Date: Mon, 23 May 2011 20:31:25 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 120 Message-ID: NNTP-Posting-Host: www2.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader1.panix.com 1306182685 5873 96.57.23.82 (23 May 2011 20:31:25 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 23 May 2011 20:31:25 +0000 (UTC) To: ruben-at-mrbrklyn.com X-Blackjet: Blackjet is a Yankee Fan X-DRMisTHEFT: Use GNU Linux today X-From: A Dark Cloud X-LOCATION: Brooklyn NY - Forget abou' it! X-NYLXS: Really - yah think computers are supposed to be broken? User-Agent: Pan/0.133 (House of Butterflies) Xref: panix comp.lang.c++:1085207
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 of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:35 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!bloom-beacon.mit.edu!micro-heart-of-gold.mit.edu!nntp.club.cc.cmu.edu!feeder.erje.net!news.ett.com.ua!not-for-mail From: "Balog Pal" Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 13:06:13 +0200 Organization: ETT newsserver Lines: 19 Message-ID: References: NNTP-Posting-Host: bc240436.catv.pool.telekom.hu Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 7bit X-Complaints-To: usenet-at-news.ett.com.ua X-Notice: Filtered by postfilter v. 0.6.1 X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 Xref: panix comp.lang.c++:1085242
"Ruben Safir" > i knew nobody would have an insight on this :(
Are you sure you want insight actually expressed?
Well, to put it straight, the code you post here would better fit thedailywtf. Most of it is as far from healthy C++ as you can get. FUBAR.
It is not clear where you got it, if it is part of some homework, please regroup, read some fundamental books on how to approach C++ ( Effective C++ and coupe of similar books, then The C++ Coding Standards), and start over from scratch.
If it is from some production, well, figure out what I would suggest...
This group is fine to help where one is stuck on some particular problem or need guidance. But who would write a book-ful of text for someone did not bother with the FAQ and the material printed long ago?
-- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:35 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!not-for-mail From: Ruben Safir Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 15:04:29 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 34 Message-ID: References: NNTP-Posting-Host: www2.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader1.panix.com 1306249469 3672 96.57.23.82 (24 May 2011 15:04:29 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Tue, 24 May 2011 15:04:29 +0000 (UTC) X-Blackjet: Blackjet is a Yankee Fan X-DRMisTHEFT: Use GNU Linux today X-From: A Dark Cloud X-LOCATION: Brooklyn NY - Forget abou' it! X-NYLXS: Really - yah think computers are supposed to be broken? User-Agent: Pan/0.133 (House of Butterflies) Xref: panix comp.lang.c++:1085259
Lets look at it this way
why does this fail to compile
template void test_me(stats::Distribution hello){ std::cout << "I recied an int and converted it to an object " << hello << std::endl; }
int main ( int argc, char *argv[] ){ int a = 7; test_me(a);
return EXIT_SUCCESS; }
yet this does, with the promotion of the int to a distribution object
template void test_me(stats::Distribution hello){ std::cout << "I received an int and converted it to an object " << hello << std::endl; }
int main ( int argc, char *argv[] ){ int a = 7; test_me(a);
return EXIT_SUCCESS; } -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:35 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!not-for-mail From: Ruben Safir Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 14:44:20 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 11 Message-ID: References: NNTP-Posting-Host: www2.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader1.panix.com 1306248260 804 96.57.23.82 (24 May 2011 14:44:20 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Tue, 24 May 2011 14:44:20 +0000 (UTC) X-Blackjet: Blackjet is a Yankee Fan X-DRMisTHEFT: Use GNU Linux today X-From: A Dark Cloud X-LOCATION: Brooklyn NY - Forget abou' it! X-NYLXS: Really - yah think computers are supposed to be broken? User-Agent: Pan/0.133 (House of Butterflies) Xref: panix comp.lang.c++:1085258
On Tue, 24 May 2011 13:06:13 +0200, Balog Pal wrote:
> This group is fine to help where one is stuck on some particular problem > or need guidance. But who would write a book-ful of text for someone did > not bother with the FAQ and the material printed long ago? >
Thanks for the follow up. It's neither homework or production. It's just a test program to shake out some Template libraries constructed as an exercise, -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:35 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!news.linkpendium.com!news.linkpendium.com!feeder.erje.net!news.ett.com.ua!not-for-mail From: "Balog Pal" Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 17:33:46 +0200 Organization: ETT newsserver Lines: 17 Message-ID: References: NNTP-Posting-Host: bc240436.catv.pool.telekom.hu Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 7bit X-Complaints-To: usenet-at-news.ett.com.ua X-Notice: Filtered by postfilter v. 0.6.1 X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 Xref: panix comp.lang.c++:1085261
"Ruben Safir" > why does this fail to compile > > template > void test_me(stats::Distribution hello); > > int main ( int argc, char *argv[] ){ > int a = 7; > test_me(a); > > yet this does, with the promotion of the int to a distribution object > test_me(a);
IIRC it is called "non-deduced context". How do you think the compiler could figure the T you meant for a general case?
-- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!news2.euro.net!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Victor Bazarov Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 11:43:26 -0400 Organization: A noiseless patient Spider Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 24 May 2011 15:43:27 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mfoyPVXLF0MtM1ZkAsRgyQ"; logging-data="31217"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/C7I8P09Pb9rAZBZkqJC1eaXCrnxP8Yw0=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:xwmh1LRs4qzK91MZjldtaTdR8fE= Xref: panix comp.lang.c++:1085263
On 5/24/2011 11:04 AM, Ruben Safir wrote: > Lets look at it this way > > why does this fail to compile > > template > void test_me(stats::Distribution hello){ > std::cout<< "I recied an int and converted it to an object "<< hello<< std::endl; > } > > > > int main ( int argc, char *argv[] ){ > int a = 7; > test_me(a); > > return EXIT_SUCCESS; > }
You ask the compiler to find what 'T' would be. During template argument deduction the compiler does NOT use any potential promotions or conversions. The fact that 'stats::Distribution' has a conversion from 'int' has *no importance* to the template argument deduction process.
> > > yet this does, with the promotion of the int to a distribution object > > > template > void test_me(stats::Distribution hello){ > std::cout<< "I received an int and converted it to an object "<< hello<< std::endl; > } > > > int main ( int argc, char *argv[] ){ > int a = 7; > test_me(a);
Here no template argument deduction is necessary since you supplied the argument.
> > return EXIT_SUCCESS; > }
V -- I do not respond to top-posted replies, please don't ask -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.cloud9consultinginc!news.posted.cloud9consultinginc.POSTED!not-for-mail NNTP-Posting-Date: Tue, 24 May 2011 12:32:59 -0500 Newsgroups: comp.lang.c++ From: "A. Bolmarcich" Subject: Re: Template argument determination References: Message-ID: User-Agent: slrn/0.9.8.0 (FreeBSD) Date: Tue, 24 May 2011 12:32:59 -0500 Lines: 65 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 168.100.1.1 X-Trace: sv3-wm4TXZbaEE7LDSQjB/jnI46v5+Y88RIU9vvafOO8Q/4qQxh346Zqeqf0ZGMV9n6B0DJv76glGnRpyP9!KxF6cSQ+ufKaUuDlocLtwRF0QdkqditdwxeUpgCZZAj7h3ADKhBoALPjKyza1kvB0LitbAedbNlP!njik9/fY4dMn79kTkKw= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2911 Xref: panix comp.lang.c++:1085272
> I'm having trouble understanding why my program is making a change in the > argument type of my template class function
A short answer is that what you are seeing is due to an implicit conversion that is part of the C++ programming language. If you do not want this implicit conversion done, declare the constructor being used by the implicit conversion as explcit, as it
class Distribution { public: explicit Distribution(int i) { // whatever the constructor does } }
What follows are details, with most of the source you gave snipped down to essentials.
> template < class T > > void List::find_value(T val) > { [snip] > }
[snip]
> 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){ [snip] > chainlist::List > * dump;
> dump->find_value(search_val); [snip] > }
[snip]
The expression
stats::mean_list(tallies, 7 )
implicitly instantiates the mean_list function template. Due to the argument types, the instantiated function includes the declaration
chainlist::List > * dump;
The expression
dump->find_value(search_val)
implicitly instantiates the find_value member function template. Due to the type of the object for which the member function is called, the instantiated member function is
void List >::find_value( stats::Distribution val)
In the call to that function by dump->find_value(search_val), an implicit conversion by constructor is done to convert the search_val argument (of type int) to a stats::Distribution (the type of the parameter). -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.cloud9consultinginc!news.posted.cloud9consultinginc.POSTED!not-for-mail NNTP-Posting-Date: Tue, 24 May 2011 14:32:36 -0500 Newsgroups: comp.lang.c++ From: "A. Bolmarcich" Subject: Re: Template argument determination References: Message-ID: User-Agent: slrn/0.9.8.0 (FreeBSD) Date: Tue, 24 May 2011 14:32:36 -0500 Lines: 12 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 168.100.1.1 X-Trace: sv3-Z7PE49lWjbeCuX5NbJyxa4tS68nK/eJ6fPlButpFYFvcxcz1qo7SUYat40NfmNE5ZRhgxe2z5tATnJZ!TECdSV6GZpylrJ4jiyt8n2WJjYB3KwSSytAup13cgQVLxsgaZbumZXWymM9oo9e4K486oK/lelZL!6AjBx8EsWc7FqihCx9U= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1518 Xref: panix comp.lang.c++:1085279
On 2011-05-24, Ruben Safir wrote:
> Bingo - thanks for taking the time to give me this explanation. But its not > capable of doing this implicit conversation unless T is defined as an int. > The compiler couldn't deduce this otherwise.
A compiler should do the implicit conversion as long as the parameter type has an accessible
- non-explicit constructor that can take a value of type T as the only argument, or - conversion function of the form: operator T() -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!not-for-mail From: Ruben Safir Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 18:08:32 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 80 Message-ID: References: NNTP-Posting-Host: www2.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader1.panix.com 1306260512 11831 96.57.23.82 (24 May 2011 18:08:32 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Tue, 24 May 2011 18:08:32 +0000 (UTC) X-Blackjet: Blackjet is a Yankee Fan X-DRMisTHEFT: Use GNU Linux today X-From: A Dark Cloud X-LOCATION: Brooklyn NY - Forget abou' it! X-NYLXS: Really - yah think computers are supposed to be broken? User-Agent: Pan/0.133 (House of Butterflies) Xref: panix comp.lang.c++:1085273
On Tue, 24 May 2011 12:32:59 -0500, A. Bolmarcich wrote:
>> I'm having trouble understanding why my program is making a change in >> the argument type of my template class function > > A short answer is that what you are seeing is due to an implicit > conversion that is part of the C++ programming language. If you do not > want this implicit conversion done, declare the constructor being used > by the implicit conversion as explcit, as it > > class Distribution { > public: > explicit Distribution(int i) { > // whatever the constructor does > } > } > > What follows are details, with most of the source you gave snipped down > to essentials. > >> template < class T > >> void List::find_value(T val) >> { > [snip] >> } > > [snip] > >> 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){ > [snip] >> chainlist::List > * dump; > >> dump->find_value(search_val); > [snip] >> } > > [snip] > > The expression > > stats::mean_list(tallies, 7 ) > > implicitly instantiates the mean_list function template. Due to the > argument types, the instantiated function includes the declaration > > chainlist::List > * dump; > > The expression > > dump->find_value(search_val) > > implicitly instantiates the find_value member function template. Due to > the type of the object for which the member function is called, the > instantiated member function is > > void List >::find_value( > stats::Distribution val) > > In the call to that function by dump->find_value(search_val), an > implicit conversion by constructor is done to convert the search_val > argument (of type int) to a stats::Distribution (the type of the > parameter).
Bingo - thanks for taking the time to give me this explanation. But its not capable of doing this implicit conversation unless T is defined as an int. The compiler couldn't deduce this otherwise.
Ruben -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!news1.as3257.net!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Victor Bazarov Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 15:48:22 -0400 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 24 May 2011 19:48:23 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mfoyPVXLF0MtM1ZkAsRgyQ"; logging-data="8638"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1+2tFtFA0g1sjXcec1a8gmFkX/hNeZsqww=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:qVibubQXx7jaWmPFnrx+WKo0UzI= Xref: panix comp.lang.c++:1085281
On 5/24/2011 3:32 PM, A. Bolmarcich wrote: > On 2011-05-24, Ruben Safir wrote: > >> Bingo - thanks for taking the time to give me this explanation. But its not >> capable of doing this implicit conversation unless T is defined as an int. >> The compiler couldn't deduce this otherwise. > > A compiler should do the implicit conversion as long as the parameter type has > an accessible > > - non-explicit constructor that can take a value of type T as the only > argument, or > - conversion function of the form: operator T()
OK, try compiling this:
template struct FromT { FromT(T) {} // here is your non-explicit c-tor };
template void foo(FromT ft) {}
int main() { foo(42); }
And then explain. Then change the line in 'main' to
foo(42);
and try again. And if the results are different, try to explain why they are.
V -- I do not respond to top-posted replies, please don't ask -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:36 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.cloud9consultinginc!news.posted.cloud9consultinginc.POSTED!not-for-mail NNTP-Posting-Date: Tue, 24 May 2011 17:11:50 -0500 Newsgroups: comp.lang.c++ From: "A. Bolmarcich" Subject: Re: Template argument determination References: Message-ID: User-Agent: slrn/0.9.8.0 (FreeBSD) Date: Tue, 24 May 2011 17:11:50 -0500 Lines: 29 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 168.100.1.1 X-Trace: sv3-uc8maIm5mAkaEyPgNj4ssSj9Y8/uTA61pCY8BIimUTrhasJZoZJfJzP6/s9+MUuZyxh+KUa94i4OUSA!seXRH9vm4Hjr/ljL/tyPvh2Fpna3cfdDMybeAgJTjAJo4r0DD26mkMIgcgfzklG47EjHXEdPDr1P!0riKg0OUlkI9zul7UTk= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1931 Xref: panix comp.lang.c++:1085293
On 2011-05-24, Victor Bazarov wrote: > OK, try compiling this: > > template struct FromT { > FromT(T) {} // here is your non-explicit c-tor > }; > > template void foo(FromT ft) {} > > int main() { > foo(42); > } > > And then explain. Then change the line in 'main' to > > foo(42); > > and try again. And if the results are different, try to explain why > they are.
For original program with foo(42), the template for foo is not instantiated because the compiler is not able to deduce the template argument to use for T.
With foo(4), the template argument to use for T is explicitly specified. The instantiated function is void foo(FromT ft). In the call to that function, the argument value 42 is implicitly converted using the FromT(int) constructor (of the struct FromT that was implicitly instantiated due to it being the type of the parameter of foo). -- end of forwarded message --
----- End forwarded message ----- ----- Forwarded message from Ruben Safir -----
Date: Tue, 24 May 2011 19:27:37 -0400 (EDT) From: Ruben Safir To: ruben-at-mrbrklyn.com Subject: (fwd) Re: Template argument determination User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386))
-- forwarded message -- Path: reader1.panix.com!panix!bloom-beacon.mit.edu!micro-heart-of-gold.mit.edu!nntp.club.cc.cmu.edu!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Victor Bazarov Newsgroups: comp.lang.c++ Subject: Re: Template argument determination Date: Tue, 24 May 2011 19:04:02 -0400 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 24 May 2011 23:04:07 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="J3EW1I9Cuyi1wJAW+L/yWA"; logging-data="23872"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX18xo8DWCPxWlva6SvzbpaZ8dNuTESDFEFY=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:DNU53VNbONVT6ZmOPUA66iatXsU= Xref: panix comp.lang.c++:1085296
On 5/24/2011 6:11 PM, A. Bolmarcich wrote: > On 2011-05-24, Victor Bazarov wrote: >> OK, try compiling this: >> >> template struct FromT { >> FromT(T) {} // here is your non-explicit c-tor >> }; >> >> template void foo(FromT ft) {} >> >> int main() { >> foo(42); >> } >> >> And then explain. Then change the line in 'main' to >> >> foo(42); >> >> and try again. And if the results are different, try to explain why >> they are. > > For original program with foo(42), the template for foo is not instantiated > because the compiler is not able to deduce the template argument to use for > T.
Perhaps I didn't understand your statement. I seems you said that the compiler was supposed to use the conversion as basis for deducing the template argument from the function call with 42. You snipped this:
> On 2011-05-24, Ruben Safir wrote: > >> > Bingo - thanks for taking the time to give me this explanation. But its not >> > capable of doing this implicit conversation unless T is defined as an int. >> > The compiler couldn't deduce this otherwise. > A compiler should do the implicit conversion as long as the parameter type has > an accessible > > - non-explicit constructor that can take a value of type T as the only > argument, or > - conversion function of the form: operator T()
Let me explain what I am reading here. Ruben: "compiler cannot use any conversion unless T is defined". You: "A compiler should do the conversion." - as if it doesn't matter whether type is deduced or not.
> [..]
Anyway, probably doesn't matter at this point.
V -- I do not respond to top-posted replies, please don't ask -- end of forwarded message --
----- End forwarded message -----
|
|