c++: Does gcc provide extended macro/function like “countof” the size of an array?

霸气de小男生 提交于 2019-12-12 02:37:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/43444235/c-does-gcc-provide-extended-macro-function-like-countof-the-size-of-an-arra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!