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_asserts, 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
Aucun commentaire:
Enregistrer un commentaire