On 14/05/2018 00:11, John Carter wrote:
> With an assert() modified to give compile-time warnings when
possible, you will generally have run-time costs.
Hmm. Not true, the __builtin_choose_expr( __builtin_constant_p(exp))
completely vanishes at run time in optimized code.
I don't mean that the __builtin_constant_p or other parts have a
run-time cost. I mean the standard assert() parts have a run-time costs
if the compiler can't determine that the condition is either always true
or always false at compile time. That means your modified assert has,
in general, a run-time cost in comparison to static_assert - not in
comparison to a traditional assert.
I agree that a modified assert is better than a traditional assert
(modulo the points you make below). What I /don't/ agree with is using
it to replace static_assert. static_assert /always/ has zero run-time
cost. Your modified version will have costs that can vary according to
optimisation settings, debug settings, and even small details in the
code that might affect the levels of inlining and therefore how much the
compiler can establish at compile time.
So use static_assert when you /know/ something should be known at
compile time, and your modified assert when you actually want a check at
run time, but with a little more static checking than a normal assert.
mvh.,
David
The only downsides are....
* it does something slightly different to the posix standard. Hence
probably should have a slightly different name.
* it's a gcc extension (but seems to work well with clang)
* it doesn't work with C++
On Fri, May 11, 2018 at 6:21 PM, David Brown <david@xxxxxxxxxxxxxxx
<mailto:david@xxxxxxxxxxxxxxx>> wrote:
On 10/05/18 00:30, John Carter wrote:
It catches things that can be inlined but seen as constant at
comile time.
I think as gcc's optimizer get's smarter this will be quite a win.
It also basically subsumes static_assert()
This does not replace static_assert. The key point about
static_assert is that it is /always/ static - zero cost at run-time
or code size. You know that is the case when you write it, and
other people reading the code know that. So static_asserts are
always free, and can be used liberally. With an assert() modified
to give compile-time warnings when possible, you will generally have
run-time costs.
--
John Carter
Phone : (64)(3) 358 6639
Tait Electronics
PO Box 1645 Christchurch
New Zealand
------------------------------------------------------------------------
This Communication is Confidential. We only send and receive email on the
basis of the terms set out at www.taitradio.com/email_disclaimer
<http://www.taitradio.com/email_disclaimer>
------------------------------------------------------------------------