gpt4 book ai didi

c - 等同于其他编译器中 MSVC 的 _countof?

转载 作者:太空狗 更新时间:2023-10-29 16:23:28 26 4
gpt4 key购买 nike

是否有任何内置的等价于 _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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com