I'm trying to use std::size instead of macros like _countof or ARRAYSIZE, but I'm running into scoping problems.
Is the following code legal?
#include <iterator>
int main()
{
int arr1[4];
auto f = [](int(&arr2)[std::size(arr1)])
{
arr2[0] = 1;
};
(void)arr1;
(void)f;
}
GCC and MSVC compile it fine, but Clang complains:
error: variable 'arr1' cannot be implicitly captured in a lambda with no capture-default specified
auto f = [](int(&arr2)[std::size(arr1)])
Which one is correct?
constexpr auto size_arr1 = std::size(arr1);and then use it in the lambda to convince clangstd::size(arr1)does not appear in an unevaluated operand, so it is potentially-evaluated andarr1is odr-used. I think the question is rather whether or not the odr-use is allowed in the parameter of the lambda (with or without implicit/explicit capture).