简体   繁体   English

零大小的数组不适用于模板

[英]Zero-sized arrays don't work with templates

Using GCC, it seems template argument substitution always fails with a zero-sized array. 使用GCC时,似乎模板参数替换始终失败,并且数组大小为零。 I would expect the static_assert to fail and print the message in test1 , just like it does with test2 . 我希望static_assert失败并在test1打印消息,就像在test2
You can also remove the static_assert s, the template does not work with the zero-sized array. 您也可以删除static_assert ,该模板不适用于零大小的数组。

Since zero-sized arrays are an extension, there surely is no rule about special treatment of them in the C++ standard, so my question is: 由于零大小数组是扩展,因此在C ++标准中肯定没有关于特殊处理的规则,所以我的问题是:
Do I miss something, is this a bug, or is this intended by the GCC authors? 我是否想念某些东西,这是一个错误还是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);
}

Error output: 错误输出:

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");
     ^

My compiler version is: 我的编译器版本是:

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

Update: I tested it with Visual C++ 2015 today, it shows the same behaviour. 更新:我今天用Visual C ++ 2015测试了它,它显示了相同的行为。 VC++ supports zero-sized arrays only if they are the last member of a class/struct, but if the code is changed accordingly, it's exactly the same as with g++: VC ++仅支持零大小的数组,只要它们是类/结构的最后一个成员,但是如果代码进行了相应的更改,则它与g ++完全相同:

The function template never compiles with zero-sized arrays. 函数模板永远不会使用零大小的数组进行编译。 Why? 为什么?

#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);
}

Do I miss something, is this a bug, or is this intended by the GCC authors? 我是否想念某些东西,这是一个错误还是GCC作者的意图?

Accepting zero-sized arrays in templates would lead to either conformance issues or an impossible-to-maintain language. 在模板中接受零大小的数组将导致一致性问题或无法维护的语言。

Given 给定

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

A call f<1>(0, 0) must use the first overload because it is a better match for the second argument. 调用f<1>(0, 0) 必须使用第一个重载,因为它与第二个参数更好地匹配。

A call f<0>(0, 0) must use the second overload because SFINAE discards the first overload due to the zero-sized array. 调用f<0>(0, 0) 必须使用第二个重载,因为SFINAE会由于数组大小为零而丢弃第一个重载。

Zero-sized arrays are allowed as an extension so long as they do not alter the semantics of any standard C++ program. 零大小的数组可以作为扩展名使用,只要它们不改变任何标准C ++程序的语义即可。 Allowing zero-sized arrays during template argument substitution would alter the semantics of a standard C++ program, unless a whole list of exceptions gets implemented where zero-sized arrays should not be allowed. 在模板参数替换期间允许零大小的数组将改变标准C ++程序的语义,除非在不允许零大小的数组的地方实现了完整的例外列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM