For example, in the main function, I can write int arr[42]
, and I can use range for loop without indicating its length for (auto i : arr)
. It works well. Compiler will know its length.
If I pass this array to another function, such as void foo(int arr[])
, I cannot use range for loop because the compiler doesn't know its length.
After all, if I have this template template<class T, size_t N> void arr_init(T (&foo)[N])
, I can call it by arr_init(x)
. Obviously, the compiler calls arr_init(int (&)[42])
.
I guess the compiler knows the length of array through its declaration int[42]
. Am I right? I've got this question when I use range for loop and learn templates. I haven't met such problem before, when I was using C.
Aucun commentaire:
Enregistrer un commentaire