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,922 of 33,346   
   =?windows-1252?Q?Daniel_Kr=FCgler?= to All   
   Re: Why does my template need a type?   
   12 Mar 13 17:47:31   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 12.03.2013 22:33, schrieb DeMarcus:   
   > Hi,   
   >   
   > Usually I want to separate the declaration from the definition also   
   > for templates, but I bumped into an interesting error when doing that   
   > recently. Consider this separation.   
   >   
   > template   
   > class A   
   > {   
   > public:   
   >      A& getMe( A& arg ); // Separated implementation below.   
   > };   
   >   
   > template   
   > A& A::getMe( A& arg )   
   > {   
   >      return *this;   
   > }   
   >   
   > int main()   
   > {   
   >      A a;   
   >      A b;   
   >      a.getMe( b );   
   >      return 0;   
   > }   
   >   
   > This code gives me on gcc 4.7.2.   
   > error: invalid use of template-name ‘A’ without an argument list   
   >   
   > But if I change the return type to look like the following, it   
   > compiles.   
   >   
   > template   
   > A& A::getMe( A& arg )   
   > {   
   >      return *this;   
   > }   
   >   
   > My question is; why do I get a compilation error for the return type   
   > but not for the argument 'arg' that is still just an A& ?   
      
   I think the reason is that in your out-of-class definition, the injected   
   class name has not been inserted at that point (Take this with a grain   
   of salt). IMO 9 p2 says:   
      
   "A class-name is inserted into the scope in which it is declared   
   immediately after the class-name is seen. The class-name is also   
   inserted into the scope of the class itself; this is known as the   
   injected-class-name."   
      
   This happens not before the "A::" in your example code. A different   
   way of fixing your problem would be to take advantage of the   
   "trailing-return-type" syntax, such as:   
      
   template   
   auto A::getMe( A& arg )-> A&   
   {   
       return *this;   
   }   
      
   Now, the return type "A&" is *after* the start of the scope and thus   
   should be accepted 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