<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 7, 2014 at 5:15 AM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">[Moving from llvmdev to cfe-dev]<br></div></blockquote><div><br></div><div>Ah. Apologies for the wrong list.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>__builtin_object_size can be used as a constant expression in some cases (essentially, when its operand is a constant expression).</div>
</div></div></div></blockquote><div><br></div><div>I was actually a little surprised that it's not always a constant expression - I would have assumed that if it wasn't a constant expression, the worst it would do is turn into a (size_t) -1 for type 0.</div>
<div><br></div><div>I'm guessing the reason is that this can only be determined at a later stage (after optimization).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>enable_if should do the job here; it's designed for exactly this sort of check. Something like:</div><div><br></div><div>void *memcpy(void *dest, const void *src, size_t n)</div>
<div>  __attribute__((enable_if(__builtin_object_size(dest, 0) < n, "selected if dest is too small"),</div>
<div>                 unavailable("dest of memcpy is smaller than size of buffer to copy"),</div><div>                 overloadable));</div><div><div>void *memcpy(void *dest, const void *src, size_t n)</div><div>

  __attribute__((enable_if(__builtin_object_size(src, 0) < n, "selected if src is too small"),</div><div>                 unavailable("src of memcpy is smaller than size of buffer to copy"),</div><div>

                 overloadable));</div></div><div><div>void *memcpy(void *dest, const void *src, size_t n) asm("memcpy")</div></div><div>  __attribute__((overloadable));</div><div><br></div><div>... should work.</div>
</div></div></div></blockquote><div><br></div><div>Ah, awesome. Will certainly try this out.</div><div><br></div><div>I was kind of hoping for something that would have been somewhat portable between GCC and Clang (even if it involved some macro magic).</div>
<div><br></div><div>Hopefully GCC picks up these attributes - feels like a much more explicit mechanism than "did this function call get optimized out". About the only downside I see is that "overloadable" probably means name mangling, though the docs mention that can be avoided by using "static inline".<br>
<br>Cheers!<br><br></div></div><br><br></div></div>