零大小数组不适用于模板

Zero-sized arrays don't work with templates

本文关键字:适用于 不适用 数组 小数      更新时间:2023-10-16

使用GCC,对于零大小的数组,模板参数替换似乎总是失败。我希望static_assert失败,并在test1中打印消息,就像在test2中一样
您也可以删除static_assert,该模板不适用于零大小的数组。

由于零大小数组是一个扩展,在C++标准中肯定没有对它们进行特殊处理的规则,所以我的问题是:
我错过了什么吗,这是一个错误,还是GCC作者有意这样做

#include <iostream>
template <size_t len>
void test1(const char(&arr)[len])
{
    static_assert(len > 0, "why am I never printed?");
}
template <size_t len>
void test2(const char(&arr)[len])
{
    static_assert(len > 10, "I will be printed");
}
int main()
{
    char arr5[5];
    test2(arr5);
    char arr0[0];
    test1(arr0);
}

错误输出:

main.cpp: In function ‘int main()’:
main.cpp:21:15: error: no matching function for call to ‘test1(char [0])’
     test1(arr0);
               ^
main.cpp:21:15: note: candidate is:
main.cpp:4:6: note: template<unsigned int len> void test1(const char (&)[len])
 void test1(const char(&arr)[len])
      ^
main.cpp:4:6: note:   template argument deduction/substitution failed:
main.cpp: In instantiation of ‘void test2(const char (&)[len]) [with unsigned int len = 5u]’:
main.cpp:18:15:   required from here
main.cpp:12:5: error: static assertion failed: I will be printed
     static_assert(len > 10, "I will be printed");
     ^

我的编译器版本是:

g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

更新:我今天用Visual C++2015测试了它,它显示出了相同的行为。只有当零大小数组是类/结构的最后一个成员时,VC++才支持它们,但如果代码相应地更改,则与g++完全相同:

函数模板从不使用零大小的数组进行编译。为什么?

#include <iostream>
struct s_arr
{
    char arr0[0];
};
template <size_t len>
void test(const char(&arr)[len])
{
}
int main()
{
    s_arr s;
    test1(s.arr0);
}

我错过了什么吗,这是一个bug,还是GCC作者有意这样做?

在模板中接受零大小的数组将导致一致性问题或不可能维护的语言。

给定

template <int N> void f(char (*)[N], int);
template <int N> void f(void *, void *);

调用f<1>(0, 0)必须使用第一个重载,因为它与第二个参数更匹配。

调用f<0>(0, 0)必须使用第二个重载,因为SFINAE由于数组大小为零而丢弃了第一个重载。

零大小数组可以作为扩展,只要它们不改变任何标准C++程序的语义。在模板参数替换期间允许零大小的数组将改变标准C++程序的语义,除非在不允许零大小数组的情况下实现了一整列异常。

相关文章: