4 ms·
What's the advantage of ever using cmp over just always using cmp_once?
by jstanley 9y ago
What's the advantage of ever using cmp over just always using cmp_once?
- deleted 9y ago[deleted]
- tbodt 9y agoIf foo and bar are functions, foo() > bar() ? foo() : bar() can have a different result than int __x = foo(); int __y = bar(); __x > __y ? __x : __y
- loeg 9y agoI think you've misread the question. Grandparent isn't asking why use cmp_once at all; they're asking, why not always force the single evaluation of x and y (i.e., always use cmp_once)?
- tbodt 9y agoAh, that's because cmp_once doesn't yield a constant even when the inputs are constant. The reason that matters is there are some arrays in the kernel declared like int things[max(size1, size2)]; and there's been recently a push to remove all VLAs (arrays with non constant size) from the kernel. The -Wvla flag was warning about these arrays even though they're actually constant size, so something needed to be done.
- loeg 9y agoYep. https://news.ycombinator.com/item?id=16720118 https://news.ycombinator.com/item?id=16720118 is another good explanation :-).
- loeg 9y agoThere's an answer in another thread: https://news.ycombinator.com/item?id=16720118 https://news.ycombinator.com/item?id=16720118
- jstanley 9y agoI still don't get it. If it can't tell that the cmp_once is a constant expression why can it tell that cmp is a constant expression? EDIT: I think I get it. In the event that the arguments are constant, something about the typeof business prevents it from being constant, so that's no good if required in a context where a constant is required. But, when not required to be a constant expression, you want to enforce single-evaluation of the arguments, so then cmp_once is used.
- loeg 9y agoI think it's the ({ }) bit (or perhaps the typeofs) that turns it from a constant expression to a constant value, but I'm not really sure. But yeah, something about cmp_once makes it the wrong kind of syntax.