https://bugs.documentfoundation.org/show_bug.cgi?id=147021

            Bug ID: 147021
           Summary: Use std::size() instead of SAL_N_ELEMENTS() macro
           Product: LibreOffice
           Version: unspecified
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: medium
         Component: LibreOffice
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]

The SAL_N_ELEMENTS() macro is defined in 'include/sal/macros.h'. It calculates
the number of elements in an array at compile time. According the comments,
"the argument must be an array and not a pointer".

These are some examples from the above file:

    SAL_N_ELEMENTS("foo");

    char aFoo[]="foo";
    SAL_N_ELEMENTS(aFoo);

This macro, uses sizeof() operator to calculate the "sizeof (arr) / sizeof
((arr)[0]))".

Now we can use std::size() instead of SAL_N_ELEMENTS() macro. The result would
be constexpr, compile-time constant expression.

As an example, this code:

    for (unsigned i = 0; i != SAL_N_ELEMENTS(aFoo); ++i)
    {
        ...
    }

can be converted into this one:

    for (unsigned i = 0; i != std::size(aFoo); ++i)
    {
        ...
    }

One pitfall would be comparing the result with a signed value. Then, either you
have to cast the result to a signed value, or use an unsigned value for the
other side of the comparison.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to