等同MSVC在其他编译器_countof? [英] Equivalents to MSVC's _countof in other compilers?

查看:226
本文介绍了等同MSVC在其他编译器_countof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何内置的等价 _countof 通过其他编译器,特别是海湾合作委员会,并提供锵?是否有任何非宏形式?

Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?

推荐答案

使用C ++ 11的非宏的形式是:

Using C++11, the non-macro form is:

char arrname[5];
size_t count = std::extent< decltype( arrname ) >::value;

范围可以在 type_traits 头被发现。

或者,如果你希望它看起来有点漂亮,在这个包装它:

Or if you want it to look a bit nicer, wrap it in this:

template < typename T, size_t N >
size_t countof( T ( & arr )[ N ] )
{
    return std::extent< T[ N ] >::value;
}

和则变成了:

char arrname[5];
size_t count = countof( arrname );

char arrtwo[5][6];
size_t count_fst_dim = countof( arrtwo );    // 5
size_t count_snd_dim = countof( arrtwo[0] ); // 6

编辑:我只注意到了C标志,而不是C ++。所以,如果你在这里为C,请您忽略此职位。谢谢你。

I just noticed the "C" flag rather than "C++". So if you're here for C, please kindly ignore this post. Thanks.

这篇关于等同MSVC在其他编译器_countof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆