如何使用模板函数计算C++数组项,同时允许空数组

air*_*man 6 c++ templates

我使用以下模板函数来计算数组项:

#include <stdio.h>

template<typename T, size_t N> constexpr
size_t countof(T(&)[N])
{
    return N;
}

int main(void)
{
    struct {} arrayN[] = {{}, {}, {}};
    printf("%zu\n", countof(arrayN));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它工作,但不是一个空数组:

struct {} array0[] = {};
printf("%zu\n", countof(array0));
Run Code Online (Sandbox Code Playgroud)

gcc 5.4输出:

error: no matching function for call to ‘countof(main()::<anonymous struct> [0])’
note: candidate: template<class T, long unsigned int N> constexpr size_t countof(T (&)[N])
note:   template argument deduction/substitution failed:
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加专业化:

template<typename T> constexpr
size_t countof(T(&)[0])
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它甚至变得怪异:

error: no matching function for call to ‘countof(main()::<anonymous struct> [0])’
note: candidate: template<class T, long unsigned int N> constexpr size_t countof(T (&)[N])
note:   template argument deduction/substitution failed:
note: candidate: template<class T> constexpr size_t countof(T (&)[0])
note:   template argument deduction/substitution failed:
note:   template argument ‘-1’ does not match ‘#‘integer_cst’ not supported by dump_decl#<declaration error>’
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

air*_*man 6

根据2011标准的第8.5.1节,"空的初始化列表{}不得用作未知边界数组的初始化子句",注意:"语法提供空的初始化列表,但是尽管如此,C++没有零长度数组".

现在我想知道为什么声明会被编译...