MESSAGE
DATE | 2011-05-24 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] template implicit converstion
|
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).
|
|