home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,333 of 33,346   
   =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to All   
   Re: confused by template type parameter    
   25 May 12 01:20:44   
   
   42bef5d3   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-05-25 06:30, 萌 朱 wrote:   
   >> Let me nit-pick first that this program is already ill-formed   
   >> because there is no guarantee for the declaration of a function sqrt   
   >> being available. I'm adding an assumed   
   >>   
   >> #include   
   >   
   > Thanks. Would it be better to #include  then?   
      
   Presumably yes, but I didn't do so, because in your original program you   
   were referring to sqrt, not std::sqrt, therefore the minimum necessary   
   change was to add . If using , you better refer to   
   std::sqrt, anything else would be non-portable.   
      
   >> You are correct. Note also that in C++03 the function call   
   >> std::pow(c,1./2) would be ambiguous, because the compiler wouldn't   
   >> find a single best match for this overload set:   
   >>   
   >> template  complex  pow(const complex&, int);   
   >> template  complex  pow(const complex&, const T&);   
   >> template  complex  pow(const complex&, const complex&);   
   >> template  complex  pow(const T&, const complex&);   
   >>   
   >> Ignoring the last one, either of   
   >>   
   >> template<>  complex  pow(const complex&, int);   
   >> template<>  complex  pow(const complex&, const float&);   
   >> template<>  complex  pow(const complex&, const   
   >> complex&);   
   >>   
   >> would be considered and none (especially of the first two) is better   
   >> than the other compared to the arguments const std::complex   
   >> and double.   
   >   
   > I have questions. I do not see how did you get the candidate   
   > functions.   
   > Especially how did you get the complex  specialization.   
   > My understanding is that for function templates, parameter deduction   
   > comes first, which for this set of function templates, is   
   > (1) for   
   >           template  complex  pow(const complex&, int);   
   >       T is deduced as float from the actual argument c and the deduction   
   >       finished, so the deduction succeeded and this adds   
   >           template<>  complex  pow(const complex&, int);   
   >       to the candidate function set.   
      
   Correct.   
      
   > (2) for   
   >           template  complex  pow(const complex&, const T&);   
   >       T is again deduced as float from c and as double from 1./2   
   >       therefore, the deduction failed as T cannot be consistently   
   >       deduced. so this one produces no functions.   
   > (3) for   
   >           template  complex  pow(const complex&, const   
   > complex&);   
   >       T is deduced as float from c and failed for 1./2 because there is   
   >       no type such that complex  would match double.  therefore,   
   >       deduction failed and this adds no function to the candidate set.   
   >   
   > Now overload resolution takes place, it sees only   
   >           template<>  complex  pow(const complex&, int);   
   >   
   > and noticed that double has implicit conversion to int. This is to say   
   > this call is valid if only those four function templates are   
   > considered.   
   > I am learning template deduction rules and how they work in action.   
   > I appreciate if you can point me with detailed explanation where I was   
   > wrong in my logic. Thanks.   
      
   You are right, my explanation was incorrect. Some library   
   implementations did indeed provide overloads of the pow template instead   
   of the declared templates. In this case the call were ambiguous. If they   
   templates were declared, the selected overload should be   
      
   template  complex  pow(const complex&, int);   
      
   as you say. And the fact that some libraries provided those overloads is   
   non-conforming, but I think this was already done to realize the   
   additional overload requests from TR1. This should teach me that my own   
   prejudice is an easy trap for another incorrect conclusion chain - my   
   apologies for that.   
      
   >> Your deduction chain is wrong.   
      
   So were mine ;-)   
      
   > Great, I did not read this part. Given this list, is there any special   
   > reason to list the three overloads of pow in [complex.syn]? I ask as   
   > this is confusing because when I read that part, I thought those three   
   > are all that offered as (it seems) in the case of c++03.   
      
   I certainly agree that the editorial representation could be improved.   
   But I think that it is general the right decision to list the templates   
   at that point, because otherwise people might miss these functions from   
   the synopsis.   
      
   >> The explicit template parameter does not solve the problem. In C++03   
   >> it would still be ambiguous because of the three overloads   
   >>   
   >> template<>  complex  pow(const complex&, int);   
   >> template<>  complex  pow(const complex&, const float&);   
   >> template<>  complex  pow(const complex&, const   
   > complex&);   
   >>   
   >> In C++11 the code behaviour is unspecified, because from above   
   >> wording in [cmplx.over] p3 there is an unspecified number of further   
   >> functions or function templates implied. Don't write it that way in   
   >> portable code.   
   >   
   > Sure, after reading [cmplx.over].   
   >   
   >> IMO this behaviour is incorrect. In C++03 this should be ambigious as   
   >> explained above.   
      
   Here I was wrong for the same reasons as mentioned above.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca