void fun (char (&a)[2]) // 1D reference
{}
template<typename T, int SIZE>
void funT (T (&a)[SIZE]) // 1D reference
{}
int main ()
{
char c[2][2]; // 2D array
fun(c); // error
funT(c); // ok !!!??
}
I can expect that fun()
gives error, but how come funT()
works fine!
Is there any reference in the standard for such behavior or Is it a bug in C++ language?
Because the type of c
isn't char [2]
, it doesn't match the first
function. In the template case, T
resolves to char [2]
, which means
that the final argument type is char (&a)[2][2]
. (You can think of it
as the T
becoming the equivalent of a typedef
to char[2]
, and
expand the argument type based on that.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With