作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是否有任何内置的等价于 _countof
的东西?由其他编译器提供,特别是 GCC 和 Clang?有没有非宏的形式?
最佳答案
使用C++11,非宏形式为:
char arrname[5];
size_t count = std::extent< decltype( arrname ) >::value;
extent
可以在 type_traits
header 中找到。
或者如果你想让它看起来更好一点,把它包起来:
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 的,请忽略这篇文章。谢谢。
关于c - 等同于其他编译器中 MSVC 的 _countof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4415530/
判断这2个相似的Uris实际上相同的标准方法是什么? var a = new Uri("http://sample.com/sample/"); var b = new Uri("http://sam
这个问题在这里已经有了答案: Why does "true" == true show false in JavaScript? (5 个答案) 关闭 5 年前。 可能我很困惑,但我无法理解这个愚蠢
我是一名优秀的程序员,十分优秀!