请解释以下返回数组大小的函数

发布于 2024-12-19 20:11:42 字数 476 浏览 4 评论 0原文

可能的重复:
有人可以解释一下吗给我数组大小的模板代码?

template <typename T,unsigned S>
unsigned ArraySize(const T (&v)[S])
{
    return S;
}

我理解 TS,但我的问题是为什么我们v 声明为引用 多变的?或者也许我误解了整件事。

我很感激你的帮助!

Possible Duplicate:
Can someone explain this template code that gives me the size of an array?

template <typename T,unsigned S>
unsigned ArraySize(const T (&v)[S])
{
    return S;
}

I understand the T and S, but my question is why do we have to declare v as a reference variable? Or maybe I'm misunderstanding this whole thing.

I appreciate the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯℡ 2024-12-26 20:11:42

该函数通过引用接受数组,因此,编译器会推导出元素类型 T 和大小 S 。所以它返回S,它只不过是数组的大小。如果没有引用,它将退化为指针类型。所以它们之间没有区别:

void f(int v[100]);  //declaration of f
void f(int v[200]);  //redeclaration of f
void f(int v[]);     //redeclaration of f
void f(int *v);      //redeclaration of f

都是完全相同的。您可以将任意大小的数组传递给所有这些函数。


回到ArraySize,该函数的返回值不能用作常量表达式:

int a[10];
SomeClassTemplate<ArraySize(a)> obj; //error

参见错误:http ://ideone.com/4mdJE

因此,更好的实现是这样的:

template <typename T,unsigned S>
char (&ArraySizeHelper(const T (&v)[S]))[S];  //no need to define it!
#define ArraySize(a) sizeof(ArraySizeHelper(a))

现在这完全没问题:

int a[10];
SomeClassTemplate<ArraySize(a)> obj; //ok

请参阅 ok : http://ideone.com/Zt3UY

The function accepts an array by reference, and because of this, type of element T and size S is deduced by the compiler. So it returns S which is nothing but the size of the array. In the absence of reference, it would decay into a pointer type. So there is no difference between these:

void f(int v[100]);  //declaration of f
void f(int v[200]);  //redeclaration of f
void f(int v[]);     //redeclaration of f
void f(int *v);      //redeclaration of f

All are exactly same. You can pass array of any size to all of these functions.


Coming back to ArraySize, the returned value of this function cannot be used as constant expression:

int a[10];
SomeClassTemplate<ArraySize(a)> obj; //error

See error : http://ideone.com/4mdJE

So a better implementation would be this:

template <typename T,unsigned S>
char (&ArraySizeHelper(const T (&v)[S]))[S];  //no need to define it!
#define ArraySize(a) sizeof(ArraySizeHelper(a))

Now this is perfectly fine:

int a[10];
SomeClassTemplate<ArraySize(a)> obj; //ok

See ok : http://ideone.com/Zt3UY

简单 2024-12-26 20:11:42

如果 v 不是引用,那么您将得到以下结果:

template <typename T,unsigned S>
unsigned ArraySize(const T v[S])
{
    return S;
}

但是在这种情况下使用 [] 只是另一种编写方式

template <typename T,unsigned S>
unsigned ArraySize(const T *v)
{
    return S;
}

,不会为您提供任何大小信息。

If v wasn't a reference, then you would have this:

template <typename T,unsigned S>
unsigned ArraySize(const T v[S])
{
    return S;
}

But the use of [] in this context is just another way of writing

template <typename T,unsigned S>
unsigned ArraySize(const T *v)
{
    return S;
}

which would give you no size information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文