c++ - Calculating amount of elements in an array with a template function -
I was looking for a way to create a template function that used to calculate the amount of elements in an array, But this is just an indicator and so I came up with this:
template & lt; Typename T & gt; Long int calculation elements (t * few more) {returns (psychof (t) / psychoph (& some array); };
Now, my sense tells me that this should not work, but it does!
I have an array with 10 integers T
a int
, so will be sizeof (T)
4
, someArray
will be an indicator for an integer array, therefore sizeof (and someArray)
10 times the size of an integer ( 10 * size (T)
), which would be 40
function should return to 0, but it is not.
Can anyone explain to me what I am doing?
Thank you.
This can work in the context of the array:
template & Lt; Typename T, std :: size_t N & gt; Constexpr std :: size_t calculation elements (CONST T (& amp;) [N]) {return n; }
.
Currently you come back to the sizeof (T) / sizeof (T **)
which is divided by the size of the element, the size of a pointer (32 bits and 8 8 bits at 64 bits).
Comments
Post a Comment