I recently wrote a function template which takes a reference to a C-array:
template <class T, size_t N>
void foo(T(&c_array)[N]);
Assuming T is a char
, the length of the C-string is N - 1
due to the null-terminator. I realized I should probably handle the edge-case where N == 0
, because then N - 1
would be std::numeric_limits<std::size_t>::max()
.
So in order to avoid the chaos that might ensue in the rare case that someone passes a zero-length array to this function, I placed a check for N == 0
.
However, to my surprise, it seems that a zero-length array is actually not even an array type - or at least, that's what GCC seems to believe. In fact, a zero-length array doesn't even bind to the above function signature, if a function with a pointer-type signature is available as a candidate.
Consider the following code:
template <class T, size_t N>
void foo(T(&array)[N])
{
std::cout << "Array" << std::endl;
}
void foo(const void* p)
{
std::cout << "Pointer" << std::endl;
}
int main(int argc, char** argv)
{
char array1[10] = { };
const char* pointer = 0;
char array2[0] = { };
foo(array1);
foo(pointer);
foo(array2);
}
With GCC 4.3.2, this outputs:
Array
Pointer
Pointer
Oddly, the zero-length array prefers to bind to the function that takes a pointer type. So, is this a bug in GCC, or is there some obscure reason mandated by the C++ standard why this behavior is necessary?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…