Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #5437
| From | Ruben Safir <ruben@mrbrklyn.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Template argument determination |
| Date | 2011-05-24 18:08 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <irgs70$bhn$1@reader1.panix.com> (permalink) |
| References | <ireg6t$5nh$1@reader1.panix.com> <slrnitnqub.1fk.aggedor@earl-grey.cloud9.net> |
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<T>::find_value(T val)
>> {
> [snip]
>> }
>
> [snip]
>
>> tallies = new chainlist::List< chainlist::List<
>> stats::Distribution<int> > *>;
>>
>> std::cout << "The standard mean for picking 7 is ==> " <<
>> stats::mean_list(tallies, 7 ) << std::endl;;
>>
>> template<typename T>
>> float mean_list(chainlist::List<
>> chainlist::List<stats::Distribution<T> >* > * tallies, T
>> search_val){
> [snip]
>> chainlist::List<stats::Distribution<T> > * 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<stats::Distribution<int> > * 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<stats::Distribution<int> >::find_value(
> stats::Distribution<int> 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<int> (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
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-23 20:31 +0000
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 02:50 +0000
Re: Template argument determination Michael Doubez <michael.doubez@free.fr> - 2011-05-24 03:48 -0700
Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 13:06 +0200
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 14:44 +0000
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 15:04 +0000
Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 17:33 +0200
Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 11:43 -0400
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 12:32 -0500
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 18:08 +0000
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 14:32 -0500
Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 15:48 -0400
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 17:11 -0500
Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 19:04 -0400
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:37 -0500
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-26 16:12 +0000
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-26 13:25 -0500
Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-25 02:47 +0000
Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-25 10:06 +0200
Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:40 -0500
csiph-web