Please consider this code:
#include <iostream>
template<typename T>
void f(T x) {
std::cout << sizeof(T) << '
';
}
int main()
{
int array[27];
f(array);
f<decltype(array)>(array);
}
Editor's Note: the original code used typeof(array)
, however that is a GCC extension.
This will print
8 (or 4)
108
In the first case, the array obviously decays to a pointer and T becomes int*
. In the second case, T is forced to int[27]
.
Is the order of decay/substitution implementation defined? Is there a more elegant way to force the type to int[27]
? Besides using std::vector?