<br><br><div class="gmail_quote">On Sun, Jul 22, 2012 at 9:49 PM, Howard Hinnant <span dir="ltr"><<a href="mailto:hhinnant@apple.com" target="_blank">hhinnant@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm playing with this code created by Scott Schurr and presented at C++Now:<br>
<br>
<a href="http://cppnow.org/session/cpp11-new-tools-for-class-and-library-authors/" target="_blank">http://cppnow.org/session/cpp11-new-tools-for-class-and-library-authors/</a><br>
<br>
#include <cstddef><br>
#include <stdexcept><br>
<br>
class str_const<br>
{<br>
const char* const p_;<br>
const std::size_t sz_;<br>
public:<br>
template<std::size_t N><br>
constexpr str_const(const char(&a)[N]) :<br>
p_(a), sz_(N-1) {}<br>
<br>
constexpr char operator[](std::size_t n)<br>
{<br>
return n < sz_ ? p_[n] : throw std::out_of_range("");<br>
}<br>
<br>
constexpr std::size_t size() { return sz_; }<br>
};<br>
<br>
template <class T = std::size_t><br>
constexpr<br>
inline<br>
T<br>
binary_const(str_const b, std::size_t n = 0, T x = 0)<br>
{<br>
return n == b.size() ? x :<br>
b[n] == ',' ? binary_const<T>(b, n+1, x) :<br>
b[n] == ' ' ? binary_const<T>(b, n+1, x) :<br>
b[n] == '0' ? binary_const<T>(b, n+1, x*2) :<br>
b[n] == '1' ? binary_const<T>(b, n+1, x*2+1) :<br>
throw std::domain_error("Only '0', '1', ',', and ' ' may be used");<br>
}<br>
<br>
int main()<br>
{<br>
constexpr str_const test("Hi Mom!");<br>
static_assert(test.size() == 7, "");<br>
static_assert(test[6] == '!', "");<br>
constexpr unsigned i = binary_const("1111,0000");<br>
return i;<br>
}<br>
<br>
$ clang++ -std=c++11 -stdlib=libc++ -O3 test.cpp -S<br>
<br>
Everything compiles and runs great (I know, I'm never happy). But when I inspect the assembly of main I see:<br>
<br>
callq __Z12binary_constImET_9str_constmS0_<br>
movl $240, %eax<br>
popq %rbp<br>
ret<br>
<br>
Now returning 240 is the right answer. But why is binary_const being called at run time? Can't this call be elided because we know it is a constexpr function (without side effects) and that it is going to return the value 240 that we've already computed at compile time?<br>
<br>
Howard<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br><div><br></div><div><br></div><div><div>Hello Howard,</div><div><br></div><div>What is the purpose of throwing an exception in a constexpr function?</div><div>Or rather... What are the implications of throwing in a constexpr function?</div>
</div><div><br></div><div>Thanks and Regards,</div><div>Fernando.</div>