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,160 of 33,346   
   =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to All   
   Re: function template explicit specializ   
   19 Apr 12 22:46:17   
   
   1a93f575   
   From: daniel.kruegler@googlemail.com   
      
   Am 19.04.2012 20:48, schrieb Johannes Sixt:   
   > On 19 Apr., 10:43, Daniel Krügler   
   > wrote:   
   >>> template   int hash (T);   
   >>> template   int hash (T t)          { return t; } // (1)   
   >>> template<>             int hash (dummy const&) { return 0; } // (2)   
   >>   
   >>> int main ()   
   >>> {   
   >>>     dummy a;   
   >>>     dummy const&b = a;   
   >>>     hash(b); // expect (2) called   
   >>> }   
   > ...   
   >>> (1) What does the standard (c++11) say about the call to hash with   
   >>> b, should T be deduced to be dummy const&, or should it be dummy   
   >>> const?   
   >>   
   >> It won't be deduced to be dummy const&, that is for sure. The rules   
   >> for selection of function templates in this context is uniformly   
   >> described by 14.8.2.1 [temp.deduct.call]. The function parameter   
   >> type P here is not a reference type, therefore we need to consider   
   >> p2:   
   >>   
   >> "If P is not a reference type: [..] If A is a cv-qualified type,   
   >> the top level cv-qualifiers of A’s type are ignored for type   
   >> deduction."   
   >>   
   >> So even though the argument type A is a cv-qualified type (The type   
   >> is 'dummy const'), this is irrelevant here. As a rule of thumb one   
   >> should remember that a function template that looks like using   
   >> "by-value" arguments, will have this way, unless you provide   
   >> explicit template parameters that would change that.   
   >   
   > Can you please explain why the type is 'dummy const', not 'dummy   
   > const&'?  The function argument b clearly is a reference. Is there   
   > some rvalue- to- lvalue conversion involved, and if so, why does it   
   > happen before the function type is completely deduced?   
      
   Yes, the *declared* type of b is a reference type, but this point is   
   irrelevant here. The compiler interprets the expression 'b' within the   
   context of a function call expression 'hash(b)'. The meaning of 'b' is   
   specified in Clause 5 [expr] p5:   
      
   "If an expression initially has the type “reference to T” (8.3.2,   
   8.5.3), the type is adjusted to T prior to any further analysis."   
      
   Therefore in regard to expression analysis we can say the following   
   about the sub-expression 'b':   
      
   a) It is an lvalue.   
      
   b) It has type dummy const.   
      
   The const-qualifier is conserved here, because dummy is a class type   
   (According to [basic.lval] p4 this does not apply to non-class types,   
   even though it does apply for array types as well).   
      
   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