simon-git: spigot (master): Simon Tatham

Commits to Tartarus hosted VCS tartarus-commits at lists.tartarus.org
Tue Apr 30 18:13:23 BST 2019


TL;DR:
  4f4a645 Turn lenof() back into a macro to remove side effects.

Repository:     https://git.tartarus.org/simon/spigot.git
On the web:     https://git.tartarus.org/?p=simon/spigot.git
Branch updated: master
Committer:      Simon Tatham <anakin at pobox.com>
Date:           2019-04-30 18:13:23

commit 4f4a645df6b05d53ced3f6634ad72d2ddd2359d4
web diff https://git.tartarus.org/?p=simon/spigot.git;a=commitdiff;h=4f4a645df6b05d53ced3f6634ad72d2ddd2359d4;hp=667ceff772cc242ec5525c3bae61ede87846be51
Author: Simon Tatham <anakin at pobox.com>
Date:   Tue Apr 30 18:12:53 2019 +0100

    Turn lenof() back into a macro to remove side effects.
    
    In commit d93404289 I turned my standard lenof macro into a C++
    template, which had the advantage of making it type-check that the
    parameter you gave it really was an _array_ and not a pointer. But
    I've just realised that that also changed the semantics, because now
    the argument to lenof is evaluated, including any side effects. For
    example, if p has type 'pointer to array', then lenof(*p++) would
    actually increment p, whereas in the original macro version, it
    didn't.
    
    That's not the semantics I wanted: I wanted lenof to behave as much as
    possible like sizeof, which means it should _never_ cause code to be
    actually executed. In particular, one use case for lenof is to wrap it
    in another macro which uses both an array and its length, along the
    lines of
    
      #define DO_SOMETHING_WITH(array) call_fn(array, lenof(array))
    
    and in that situation, you definitely want any side effects in the
    macro parameter to be evaluated exactly once.
    
    So, now lenof is back to its original macro definition using sizeof -
    except that inside the sizeof there's also a 'call' to a template
    function that will perform the same error check as I had in the pure
    template version. This should give me the upsides of both techniques.

 spigot.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)



More information about the tartarus-commits mailing list