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 33,135 of 33,346   
   Chris Uzdavinis to 8604spr...@googlemail.com   
   Re: Order of template function overloads   
   22 Jul 13 10:18:40   
   
   From: cuzdav@googlemail.com   
      
   On Friday, July 19, 2013 7:02:58 PM UTC-5, 8604spr...@googlemail.com wrote:   
   > On Friday, July 19, 2013 12:20:07 AM UTC-7, Chris Uzdavinis wrote:   
      
   > > It has to do with what is visible at the point of the function   
   > > definition.  It's not even a template issue, nor a c++11 issue.   
   >   
   > Is that true? My (possibly incorrect) understanding is that   
   > templates are special in that instantiation points are the only ones   
   > that matter. For example, a slightly modified version of the   
   > original code shows this pretty clearly:   
      
   > #include    
   > #include    
      
   > template  struct A {   
   >   static void f() { std::cout << "generic\n"; } };   
   > template  void a( const T & ) { A::f(); }   
   > // Defined after definition of a()   
   > template  struct A > {   
   >   static void f() { std::cout << "tuple\n"; } };   
   > template  void b( const T & ) { A::f(); }   
   >   
   > int main( int, char *[] )   
   > {   
   >   a( std::make_tuple( 1.1, 1 ) ); // prints "tuple"   
   >   b( std::make_tuple( 1.1, 1 ) ); // prints "tuple"   
   > }   
      
   I believe you're demonstrating something different.  In the original   
   example, we were talking about adding function *overloads*, which   
   expands the set of names for consideration when the compiler does   
   name-lookup.  (And explained why the set of names is fixed at the   
   function's point of definion, to avoid problems, include violations of   
   ODR as Francis pointed out.)   
      
   However, your example is something else.  You are specializating a   
   template, but that is not adding new names to the set for name-lookup.   
   Both function a() and b() only see a single name, A<>::f, and select it.   
      
   At a later point the compiler goes through the template   
   specializations to select the best template to match the name at the   
   point where it's used.   
      
   That is, all specializations of a template are irrelevant to name lookup,   
   they're just possible replacements for the primary template, but they   
   all have the same "name".   
      
   Another example:   
      
   void f(int) { }   
      
   template  f(T) { }   
   template <> f(int) {}   
      
   There are only 2 names, f(), and f<>().  If the compiler selects the   
   template version of f(), then later it'll decide WHICH template best   
   fits, and only in this later stage with f ever be considered.   
      
      
   Chris   
      
      
   --   
         [ 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