内置数组大小的类型是什么? [英] What is the type of a built-in array's size?

查看:53
本文介绍了内置数组大小的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在推导非类型模板参数(例如 n )时, n 的类型必须在参数和参数之间完全匹配.因此,以下代码将无法编译(至少在GCC和clang上):

When deducing a non-type template parameter, say n, the types of n must match exactly between the parameter and argument. So the following code will not compile (at least on GCC and clang):

#include <array>
template <int n> void f(std::array<int, n> const&) {}
int main() {
  std::array<int, 3> arr;
  f(arr);
}

这是因为 std :: array 被声明为

template <typename T, std::size_t n> class array;

而不是

template <typename T, int n> class array;

但是,要捕获内置数组的大小,似乎任何整数类型都可以.以下所有有关GCC,clang和VC ++的工作:

However, to capture the size of a built-in array, it seems that any integral type does. All of the following work on GCC, clang, and VC++:

template <typename T, char n > void f(T (&)[n]);
template <typename T, short n> void f(T (&)[n]);
template <typename T, int   n> void f(T (&)[n]);
...

那么,真的,内置数组大小的类型超载了吗?

So, seriously, the type of a built-in array's size is overloaded?

推荐答案

在14.8.2.5 [temp.deduct.type] 第17段中对此进行了介绍:

This is covered in 14.8.2.5 [temp.deduct.type] paragraph 17:

17-[...] [...]模板参数类型应与模板参数的类型完全匹配,但从数组绑定推导的模板参数可以是任何整数类型.

17 - [...] [The] template-argument type shall match the type of the template-parameter exactly, except that a template-argument deduced from an array bound may be of any integral type.

http://wg21.cmeerw.net/cwg/issue1770 中,此功能已改进为更一般:

In http://wg21.cmeerw.net/cwg/issue1770 this is improved to the more general:

17-如果 P 具有包含< i> 的形式,并且 A 的对应值的类型不同于 i 的类型,推导失败.如果 P 具有包含 [i] 的形式,并且如果 i 的类型不是整数类型,则推论失败.

17 - If P has a form that contains <i>, and if the type of the corresponding value of A differs from the type of i, deduction fails. If P has a form that contains [i], and if the type of i is not an integral type, deduction fails.

因此可以将数组边界推导为任何整数类型,但是必须在模板定义中将非类型模板参数推导为实际类型.

So array bounds can be deduced to any integral type, but non-type template parameters must be deduced to the actual type in the template definition.

的确,在C ++ 11版本的Standard中,没有地方指定数组边界的首选类型.数组边界(在 [dcl.array] 中)指定为"整数常量表达式,其值应大于零".在C ++ 14的最新草案中,将其修改为"类型为 std :: size_t [...] 的转换后的常量表达式(5.19)";更改的定义可以追溯到 n3306 .有点奇怪的是,该更改以"相应调整[..]为了保持一致"的形式表示,这意味着编辑者认为 size_t 是正确的类型.

Indeed, there is nowhere in the C++11 version of the Standard that specifies a preferred type for array bounds; an array bound is specified (in [dcl.array]) to be "an integral constant expression and its value shall be greater than zero". In recent drafts for C++14 this is amended to "a converted constant expression (5.19) of type std::size_t [...]"; the changed definition can be traced to n3306. Somewhat oddly, the change is presented as a "consequential adjustment [..] for the sake of consistency", implying that the editors considered it self-apparent that size_t was the correct type.

这篇关于内置数组大小的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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