开发者

Zero-length C-array binds to pointer type

I recently wrote a function template which takes a referen开发者_StackOverflow中文版ce 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?


As arrays must have greater than zero length, if your compiler erroneously accepts a definition of a zero-sized array then you're "safely" outside of the scope of the language standard. There's no need for you to handle the edge case of N == 0.

This is true in C++: 8.3.5 [dcl.array]: If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.


Apparently ISO C forbids 0-length arrays, which is probably affecting how GCC tries to compile stuff. See this question for further details! zero length arrays vs. pointers


The GCC Manual has a whole thing on zero length arrays. This is a GCC extension as is somewhat analogous to incomplete arrays.


Speaking for C (and probably also C++ in this case), defining a zero-length array is undefined behavior, so GCC probably does this because a) nothing's stopping it, and b) it prevents errors like the ones you're trying to avoid.


Zero-length arrays do not exist in C++. However if they did, here is how you could handle the case:

template <bool B, typename T>
struct disable_if;

template <typename T>
struct disable_if<false, T>
{
    typedef T type;
};

template <class T, size_t N>
typename disable_if<N == 0, void>::type foo(T(&c_array)[N])
{
    std::cout << "inside template\n";
}


Zero sized array are only legal in C as the last element of atruct. Anything else is pointless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜