Using GCC, it seems template argument substitution always fails with a zero-sized array. I would expect the static_assert
to fail and print the message in test1
, just like it does with test2
.
You can also remove the static_assert
s, the template does not work with the zero-sized array.
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:
Do I miss something, is this a bug, or is this intended by the GCC authors?
#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. 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++:
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);
}
Accepting zero-sized arrays in templates would lead to either conformance issues or an impossible-to-maintain language.
Given
A call
f<1>(0, 0)
must use the first overload because it is a better match for the second argument.A call
f<0>(0, 0)
must use the second overload because SFINAE discards the first overload due to the zero-sized array.Zero-sized arrays are allowed as an extension so long as they do not alter the semantics of any standard C++ program. 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.