So I was just trying to get this function to work for array sizes why wouldn't this work? I know I'm doing something stupid also what could be done for a function that does this instead?
When an array is passed by value to a function then it is converted to a pointer to its first element. So the following function declarattions are equivalent and declare the same function:
int Get_Int_Array_Length( int test[20] );
int Get_Int_Array_Length( int test[10] );
int Get_Int_Array_Length( int test[5] );
int Get_Int_Array_Length( int test[] );
int Get_Int_Array_Length ( int *test );
sizeof ( int * ) usually equal to 4 in 32-bit systems or 8 in 64-bit systems.
Thanks vlad. Well this is a good learning lesson for me then I've been playing around with it. How then would you output an arrays length of variable size in a function like that?
It does obviously work but why listen to what's going on in my head here so I understand it. So You are using a template which I understand so it could technically be any datatype correct not just an int?
And you have a reference its memory address with &test but then what's then int(&test)[N] so it's like an array it would seem of arrays almost or something? I just don't get that?
I thought with templates you declared the type later with something like <int> in replacement of T or whatever your data was? I'm just not seeing how this is working? Thanks in advance friend.
See I didn't know you could do that I thought that you had to declare templates using the datatype for each template typename later within <> like <string for T which does work like
I thought that you had to declare templates using the datatype for each template typename later within <>
The compiler is often able to determine that automatically, so you do not always have to specify that. You only have to specify it when the compiler is unable to.
@adn258
but you are not using that with N so apparently you don't have to do that with N right or? I'm still a bit confused
In your original post you are speaking about an array of type int[]. Even your function was named as Get_Int_Array_Length. So there is no need to introduce a template parameter for the type of array elements. The only template parameter that is needed is the parameter for the size of an array.
Vlad from moscow thanks man. See this is a little but confusing because I'm used to using generics in C# more than templates in C++ and while they have similarities you can't do that in C# like (&) [N] so it looks like in in C++ you can do almost anything with templates like that? That's amazing