Use the reference type for the parameter
template<typename T> void f(const T& x)
{
std::cout << sizeof(T);
}
in which case the array type will not decay.
Similarly, you can also prevent decay in your original version of f
if you explicitly specify the template agument T
as a reference-to-array type
f<int (&)[27]>(array);
In your original code sample, forcing the argument T
to have the array type (i.e. non-reference array type, by using typeof
or by specifying the type explicitly), will not prevent array type decay. While T
itself will stand for array type (as you observed), the parameter x
will still be declared as a pointer and sizeof x
will still evaluate to pointer size.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…