为什么数组匹配std :: extent第二个imp?

时间:2017-03-16 04:21:56

标签: c++ c++11 stl template-meta-programming

gnuc ++ 4.8.2 extent实现如下:

/// extent

template<typename, unsigned _Uint>
struct extent: public integral_constant<std::size_t, 0>
{static const int _ty = 1;};

template<typename _Tp, unsigned _Uint, std::size_t _Size>
struct extent<_Tp[_Size], _Uint>
: public integral_constant<std::size_t,_Uint == 0 ?
_Size : extent<_Tp,_Uint - 1>::value>
{ static cosnt int _ty =2; };

template<typename _Tp, unsigned _Uint>
struct extent<_Tp[], _Uint>: public integral_constant<std::size_t,
_Uint == 0 ? 0 : extent<_Tp,_Uint - 1>::value>
{ static cosnt int _ty =3; };

typedef int arr[2][3][3];

cout << extent<arr>::_ty是2。

为什么数组匹配第二个工具?什么是_Tp[]

我想知道extent的工作原理:o

1 个答案:

答案 0 :(得分:1)

你实际上在做:

std::extent<int[2][3][3]>

在第二个模板中,实例化范围接受具有已知大小( _Tp [_Size ])的数组的模板参数。 您所拥有的。

在第三个模板中,实例化范围接受具有未知大小( _Tp [] )的数组的模板参数。 不是 你拥有的东西。

现在,对于第一个模板,更明显的是这是一个默认的&#39;使用非数组类型实例化范围时要使用的模板。 不是 你拥有的东西。

要回答第二个问题, _Tp [] 表示未知大小的数组类型,例如 int []