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,933 of 33,346   
   DeMarcus to Seungbeom Kim   
   Re: Why does my template need a type?   
   14 Mar 13 04:41:13   
   
   From: use_my_alias_here_at_hotmail_com@tellus.orb.dotsrc.org   
      
   On 2013-03-13 08:23, Seungbeom Kim wrote:   
   > On 2013-03-12 16:47, Daniel Krügler wrote:   
   >> Am 12.03.2013 22:33, schrieb DeMarcus:   
   >>>   
   >>> template   
   >>> A& A::getMe( A& arg ) { ... }   
   > [...]   
   >>>   
   >>> 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).   
   >   
   > Right. More intuitively, consider this example:   
   >   
   >       struct A   
   >       {   
   >           typedef A* iterator;   
   >           iterator foo(iterator);   
   >       };   
   >   
   >       iterator A::foo(iterator) { ... }       // error   
   >       A::iterator A::foo(iterator) { ... }    // OK   
   >   
   > You're not in the scope of A until after "A::foo", so you need the   
   > prefix "A::" for the preceding return type.   
   >   
   >> 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.   
   >   
   > This applies to my example as well:   
   >   
   >       auto A::foo(iterator) -> iterator { ... }   // OK   
   >   
      
   Thanks both of you for great examples!   
      
   But what's the recommendation for the best practice? Would that be to   
   provide T everywhere, like the following? Or is it a personal taste   
   thing?   
      
   template   
   A& A::getMe( A& arg )   
   {   
        return *this;   
   }   
      
      
   Thanks,   
   Daniel   
      
      
   --   
         [ 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