VC provides a macro called "_countof" inside "stdlib.h", will calculates in compile time, the number of an array's elements.
My question is does gcc provide a similar utility? Thanks.
VC provides a macro called "_countof" inside "stdlib.h", will calculates in compile time, the number of an array's elements.
My question is does gcc provide a similar utility? Thanks.
In C++17, the std::size
function template will return the size of an array or container.
A possible implementation is given, so you can write your own until C++17 becomes available:
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}
This will work on any conforming C++11 compiler, so it is a lot more portable than compiler-specific extensions.