https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92293
Bug ID: 92293
Summary: No reason given for template argument deduction
failure with zero-length array
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
As an extension G++ allows zero-length arrays, but doesn't allow a length of
zero to be deduced:
using size_t = decltype(sizeof(0));
template<typename T, size_t N> void f(T(&)[N]) { }
void g()
{
int a[0];
f(a);
}
This prints:
a0.cc: In function 'void g()':
a0.cc:8:6: error: no matching function for call to 'f(int [0])'
8 | f(a);
| ^
a0.cc:3:37: note: candidate: 'template<class T, long unsigned int N> void f(T
(&)[N])'
3 | template<typename T, size_t N> void f(T(&)[N]) { }
| ^
a0.cc:3:37: note: template argument deduction/substitution failed:
Note that there is no reason given after the final note.
If you add -pedantic you get a warning on the declaration of the zero-length
array (as expected) and also the reason for deduction failure is shown
(twice!):
a0.cc: In function 'void g()':
a0.cc:7:9: warning: ISO C++ forbids zero-size array 'a' [-Wpedantic]
7 | int a[0];
| ^
a0.cc:8:6: error: no matching function for call to 'f(int [0])'
8 | f(a);
| ^
a0.cc:3:37: note: candidate: 'template<class T, long unsigned int N> void f(T
(&)[N])'
3 | template<typename T, size_t N> void f(T(&)[N]) { }
| ^
a0.cc:3:37: note: template argument deduction/substitution failed:
a0.cc: In substitution of 'template<class T, long unsigned int N> void f(T
(&)[N]) [with T = int; long unsigned int N = 0]':
a0.cc:8:6: required from here
a0.cc:3:37: warning: ISO C++ forbids zero-size array [-Wpedantic]
a0.cc:3:39: warning: ISO C++ forbids zero-size array [-Wpedantic]
3 | template<typename T, size_t N> void f(T(&)[N]) { }
| ^~~~~~~
Should that final pedwarn always be printed when showing the reason for
deduction failure, even without -pedantic?