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,099 of 33,346   
   =?ISO-8859-15?Q?Daniel_Kr=FCgler?= to All   
   Re: Implicit move of an lvalue   
   05 Apr 12 11:30:27   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 05.04.2012 20:11, schrieb GM:   
   > On 04/04/2012 20:54, Daniel Krügler wrote:   
   >   
   >> template   
   >> typename std::enable_if::value>::type   
   >> f(Type&& that)   
   >> {   
   >> A b(std::move(that));   
   >> }   
   >>   
   >> namespace details {   
   >> template   
   >> struct never {   
   >> static const bool value = false;   
   >> };   
   >> }   
   >>   
   >> template   
   >> typename std::enable_if::value>::type   
   >> f(Type&& that)   
   >> {   
   >> static_assert(details::never::value, "Sorry, no lvalues   
   >> accepted!");   
   >> }   
   >   
   > I'm not expert, so can you please explain me why this 2 simpliest   
   > solution (that booth works with my compiler MSVC++10) are not good?   
   >   
   > Solution 1 - why not directly use static_assert(false...) instead of   
   > detail::never ?   
   >   
   > template   
   > typename std::enable_if::value>::type   
   > f(Type&& that)   
   > {   
   > static_assert(false, "Sorry, no lvalues accepted!");   
   > }   
      
   This is no good solution, because a compiler is allowed to check all   
   non-dependent expressions within a template. So a compiler may reject   
   this code even if f is never instantiated. The technical correct term   
   for this situation is "ill-formed, but no diagnostic required". My   
   helper template ensures that that the expression within f is still   
   dependent.   
      
   > Soultion 2 - (the simpliest) why not directly use static_assert with   
   > !std::is_lvalue_reference without the enable_if machinery?   
   >   
   > template   
   > void f(Type&& that)   
   > {   
   > static_assert(!std::is_lvalue_reference::value, "Sorry, no lvalues   
   > accepted!");   
   > A b(std::move(that));   
   > }   
      
   Yes, this works and is much simpler.   
      
   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