数组大小元函数 - 是在提升的地方? [英] Array size metafunction - is it in boost somewhere?

查看:106
本文介绍了数组大小元函数 - 是在提升的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个博客以下模板:

I found the following template on a blog:

template <typename T, size_t N>
struct array_info<T[N]>
{
    typedef T type;
    enum { size = N };
};

这是一个优雅的替代的sizeof(A)/ sizeof的(A [0])

对于得到一个数组的大小应该肯定是介于库中的常用结构。我不知道一个。谁能告诉我这个功能在标准库的地方和/或升压?在preferably一个易于使用和轻量级形式。

A commonly-used construct for getting the size of an array should surely be somewhere in a library. I'm not aware of one. Can anyone tell me this functionality is in the standard libraries somewhere and/or in Boost? Preferably in an easy-to-use and lightweight form.

推荐答案

我终于找到自己的答案 - 的boost ::大小()

I eventually found the answer myself - boost::size():

#include <boost/range.hpp>

int array[10];
boost::size(array); // returns 10

这篇关于数组大小元函数 - 是在提升的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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