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.
(In reply to Hossein from comment #0) > 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. If there's need, we can make available C++20 > template<class T, ptrdiff_t N> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; as o3tl::ssize for the time being. (Also, if it is known that the other side of the comparison, even though of signed type, must be non-negative, like e.g. a call to OUString::getLength, then o3tl::make_unsigned from o3tl/safeint.hxx is a good choice.)
Note also, that there are cases where std::size can't give a compile-time constant, unlike the SAL_N_ELEMENTS macro. That may be e.g. when a class contains an array member, and a static_assert checks the array dimension in its constructor. In that case, a "'this' can't be used in constant expression" error would result after the replacement. Usually, there's a way to restructure such cases, to avoid use of static asserts, and ensure the tested invariant some other way.
VaibhavMalik4187 committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/8b327cd86d71d71d2f5f883321e5d53e3b42ed4e tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Hossein committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/a5c6fbe247ee9f9b2fba828d1360748c3fe4642b tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
(In reply to Commit Notification from comment #4) > Hossein committed a patch related to this issue. > It has been pushed to "master": This is an example for changing the type of the variable to a suitable unsigned type. https://git.libreoffice.org/core/commit/a5c6fbe247ee9f9b2fba828d1360748c3fe4642b The signed loop index variable of type 'int' is converted into 'size_t' to match the return value of std::size() which is unsigned
Gautham Krishnan committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/51fb84829afbc1c0957fd1a489085613ad199f1a tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
psidiumcode committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/5b320aa058504becae6ac6746926b481b326a24d tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Sinduja Y committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/a40ed687a3e2f8e1d17705fcd7d397ba09398cb6 tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Pragat Pandya committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/6abc09926c9b55a445b906303f56c6ec7fdeabf9 tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Roman Kuznetsov committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/d694b572c7262a326ac7ceac2e558f3fc6c78df4 tdf#147021 Use std::size() instead SAL_N_ELEMENTS macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Roman Kuznetsov committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/81aad6d53fa792dd95732e401ecb298714f699f4 tdf#147021 Use std::size() instead SAL_N_ELEMENTS macro It will be available in 7.4.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
jsala committed a patch related to this issue. It has been pushed to "master": https://git.libreoffice.org/core/commit/2c68c419c1fce6de1a81e1f13a84b7069125a204 tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro It will be available in 7.5.0. The patch should be included in the daily builds available at https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: https://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.