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,523 of 33,346   
   =?windows-1252?Q?Daniel_Kr=FCgler?= to All   
   Re: Is this kind of static polymorphism    
   05 Sep 12 00:48:13   
   
   From: daniel.kruegler@googlemail.com   
      
   [2nd attempt after 24 hours]   
      
   Am 03.09.2012 21:17, schrieb DeMarcus:   
   [..]   
   > I'm experimenting with static polymorphism and came up with the   
   > following. This example do run under gcc 4.7.1 but without luck I've   
   > tried to find the paragraphs in the standard that allow this kind of   
   > static polymorphism.   
   >   
   >   
   > template   
   > class Runner   
   > {   
   > public:   
   >      void run()   
   >      {   
   >         runPrivate();   
   >      }   
   >   
   > private:   
   >      void runPrivate();   
   > };   
   >   
   >   
   > namespace   
   > {   
   >      // (struct SomeRunnerLabel without declaration)   
   >      Runner r;   
   > }   
   >   
   > template<>   
   > void Runner::runPrivate()   
   > {   
   >      std::cout << "Static polymorphism" << std::endl;   
   > }   
   >   
   >   
   > int main( int argc, char* argv[] )   
   > {   
   >      r.run();   
   >      return 0;   
   > }   
   >   
   >   
   > My questions are:   
   >   
   > 1. Am I allowed to use struct SomeRunnerLabel as I do here?   
      
   Yes.   
      
   > SomeRunnerLabel is not even a forward declaration and it just serves   
   > as a label to get the polymorphism. Does anyone know where in the   
   > standard I can find the paragraph that allows this kind of template   
   > argument?   
      
   I would start with 7.1.6.3 [dcl.type.elab] p2:   
      
   "3.4.4 describes how name lookup proceeds for the identifier in an   
   elaborated-type-specifier. [..] If the elaborated-type-specifier is   
   introduced by the class-key and this lookup does not find a previously   
   declared type-name, [..] the elaborated-type-specifier is a declaration   
   that introduces the class-name as described in 3.3.2."   
      
   3.4.4 [basic.lookup.elab] p2 says:   
      
   "If the elaborated-type-specifier has no nested-name-specifier, and   
   unless the elaborated-type-specifier appears in a declaration with the   
   following form:   
   class-key attribute-specifier-seqopt identifier ;   
   the identifier is looked up according to 3.4.1 but ignoring any non-type   
   names that have been declared. [..] If the elaborated-type-specifier is   
   introduced by the class-key and this lookup does not find a previously   
   declared type-name, [..] the elaborated-type-specifier is a declaration   
   that introduces the class-name as described in 3.3.2."   
      
   Finally we have 3.3.2 [basic.scope.pdecl] p6:   
      
   "The point of declaration of a class first declared in an   
   elaborated-type-specifier is as follows:   
   — for a declaration of the form   
   class-key attribute-specifier-seqopt identifier ;   
   the identifier is declared to be a class-name in the scope that contains   
   the declaration, otherwise [..]"   
      
   3.4.1 does not apply, because we have no previously declared type of   
   that name, thus we follow this change and find that the effect is that   
   the name SomeRunnerLabel is introduced into the scope of the unnamed   
   namespace.   
      
   > 2. Is this kind of static polymorphism allowed at all? As we see, the   
   > template parameter T is not used for anything but creating multiple   
   > versions of the class, doing polymorphism on the runPrivate()   
   > function.  Also for this one I would be very happy if someone could   
   > direct me to the paragraph that allows this kind of polymorphism.   
      
   It depends what you want to do. If the unnamed namespace is part of a   
   shared header, different translation units will see different   
   SomeRunnerLabel and different objects r. Is this what you want?   
      
   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