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,151 of 33,346   
   8604sprucemill@googlemail.com to All   
   Re: Order of template function overloads   
   27 Jul 13 23:40:36   
   
   > In essence, what is important for the observed behaviour in your two example   
   > code snippets is that:   
   >   
   > 1) a template function and a template class (i.e. the particular   
   > specializations created by means of automatic instantiation from the   
   > respective function/class templates - a process driven by the compiler and   
   > resulting in generation of a class instance/object or a function definition)   
   > are instantiated at different locations with respect to the entity that   
   > enforced their instantiation:   
   >    a) "C++ defines the POI for a reference to a nonclass specialization   
   >        to be immediately AFTER the nearest namespace scope declaration or   
   >        definition that contains that reference."   
   >   
   >    b) "[T]he POI for a reference to a generated class instance is defined   
   >        to be the point immediately BEFORE the nearest namespace scope   
   >        declaration [or] definition that contains the reference   
   >        to that instance.";   
   >   
   > Additionally, these also may play role in particular situations:   
   >   
   > 2) multiple POIs for the same instance in a translation unit may exist   
   > (incl. when specializations are deliberately created by the programmer   
   > through explicit instantiation directive or explicit specialization): "For   
   > class template instances, only the first POI in each translation unit is   
   > retained, and the subsequent ones are ignored (they are not really   
   > considered POIs). For nonclass instances, all POIs are retained." Here a   
   > problem arises: the retained POIs might not be equivalent to each other,   
   > which could result in an unexpected behaviour depending on which POI the   
   > compiler will choose for the actual instantiation;   
   >   
   > 3) different implementation strategies amongst compilers as concerned the   
   > location of a POI for the actual instantiation of noninline function   
   > templates - generally postponed to the end of the translation unit.   
      
   Thank you for the detailed and thorough explanation. If I understand it   
   correctly, there is no reliable way to overload functions in a namespace   
   that is not controlled by me:   
      
   // not_under_my_control.h   
   namespace not_under_my_control {   
   template  struct X { ... };   
   template  int foo( T arg ) { ... }   
   template  void bar( T arg ) { do_something( foo( arg) ); }   
   }   
      
   // under_my_control.h   
   namespace not_under_my_control {   
   template  struct X; // forward declaration   
   template  struct X > { ... };   
   template  int foo( X > arg ) { ... }   
   }   
      
   There is no way to make bar() use the overload of foo() from   
   under_my_control.h without being able to control the include order of   
   the headers in all client code.   
      
   My particular use case is to overload std::abs for my own data type.   
   Unfortunately, for doubles/floats, most code in the wild uses a fully   
   qualified call to std::abs and hence ADL is not usable with my own   
   data type. I wonder if a standardized way to provide extension points   
   for functions in the std namespace could be created.   
      
   Regards,   
   Ravi   
      
      
   --   
         [ 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