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 31,347 of 33,346    |
|    =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to Ricardo Costa    |
|    Re: compile-time computations in C++11    |
|    11 May 11 13:19:55    |
   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-05-11 02:17, Ricardo Costa wrote:   
   >   
   > Wouldn't static_assert be better than using throw for constexpr error   
   > reporting?   
      
   I don't see why and how. A constexpr function can be used as any other   
   function as well, thus the parameters are not usable in constant   
   expressions within the body of the constexpr function:   
      
   constexpr bool f(int val)   
   {   
    static_assert(val >= 0, "NoNo"); // #   
    return val == 42;   
   }   
      
   The line marked with the # is ill-formed, because 'val' is not a   
   constant expression. This makes sense, because else we could never use   
   such a function with non-constant arguments.   
      
   In regard to the "why" I still don't get what the advantage of   
   static_assert would be compared to an exception: If a constexpr function   
   is called within a constant expression and if the code path would enter   
   the "throw", this would violate the constant expression requirements and   
   thus the code is ill-formed anyway - this is not much different than a   
   static_assert. Adapted to the above example:   
      
   constexpr bool f(int val)   
   {   
    return val >= 0 ? val == 42 : throw "Argument must be non-negative";   
   }   
      
   constexpr auto t1 = f(1); // OK, t1 == false   
   constexpr auto t2 = f(-1); // Compile error   
      
   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