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,774 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: SFINAE, deduction vs. instantiation   
   31 Dec 12 06:33:36   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 31.12.2012 08:00, schrieb Noah Roberts:   
      
   > This code fails to compile in most compilers but at first I   
   > intuitively expected SFINAE to protect me:   
      
   Your assumption is not grantedby the standard, see below.   
      
   > typedef void (*A)();   
   >   
   > template < typename T >   
   > struct a_metafun { typedef typename T::type type; };   
   >   
   > template < typename T >   
   > typename a_metafun::type f(T) {}   
   >   
   > template < typename T>   
   > void f(T(*)()) {}   
   >   
   > int main() { f(A()); }   
      
   [..]   
      
   > Upon looking at 14.8.2 (C++03) it looks to me like it specifies   
   > exactly under what conditions SFINAE can apply.  Is there a better   
   > place to look?  Failure within the instantiation of an already   
   > deduced template, even during the deduction of another, would not   
   > seem to be included in this list.   
      
   14.8.2 is basically the right place, yes. But you seem to assume a   
   specific mechanism that is not granted, in particular you seem to   
   assume that the compiler can first find the "correct" function   
   template and instantiate only that one. This is not required (and   
   sometimes not possible in general).   
      
   Note also that 14.7.1 p6 says:   
      
   "If the overload resolution process can determine the correct function   
   to call without instantiating a class template definition, it is   
   unspecified whether that instantiation actually takes place."   
      
   p9 of the very same sub-clause says:   
      
   "If a function template or a member function template specialization   
   is used in a way that involves overload resolution, a declaration of   
   the specialization is implicitly instantiated (14.8.3)."   
      
   14.8.3 describes what happens during overload resolution and from all   
   these quotes we can conclude that the compiler may instantiate both   
   *declarations* of   
      
   template < typename T >   
   typename a_metafun::type f(T);   
      
   and   
      
   template < typename T>   
   void f(T(*)());   
      
   The first overload is not sfinae-safe, because the instantiation error   
   happens within the class template definition.   
      
   > Another direction I've taken to interpreting what makes this illegal   
   > is that the deduction of a_metafun has already taken place and the   
   > instantiation of its innards is what is causing the error.   
      
   This is exactly the problem here, yes.   
      
   > SFINAE doesn't apply during instantiation but only during deduction,   
   > or am I wrong there?  In the second case though, a_metafun is being   
   > correctly, and well-formedly instantiated but it simply has no   
   > "type" definition within, which means the template attempting to   
   > instantiate it is failing due to substitution.   
      
   Overload resolution may need to instantiate more than a single   
   declaration before instantiating the actually needed definition, as   
   described above.   
      
   > Basically I'm wondering what in the standard specifies the behavior   
   > I'm witnessing.   
      
   I think that my three quotes already give quite specific wording that   
   explains the behaviour here. I have not quoted the part in 14.8.2,   
   because this is the one you have already found by yourself.   
      
   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