1

Can someone explain to me why is this piece of code working (return size of a static array):

template<class T, size_t N>
size_t lenGood(const T (& arr) [N])
{
    return N;
}

And this piece don't (decays to pointer I guess):

template<class T, size_t N>
size_t lenBad(const T arr[N])
{
    return N;
}

Sample main:

int main()
{
    int arr[] = { 10,11,8,5,4,4 };

    auto sizeGood = lenGood(arr);
    //auto sizeBad = lenBad(arr); // won't compile: no instance of function matches for argument list

    return 0;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Amadeusz
  • 1,488
  • 14
  • 21
  • 4
    Strongly related: https://stackoverflow.com/q/7177221/1207195 – Adriano Repetti Oct 06 '17 at 11:06
  • 1
    Though strongly related, this is probably more helpful: [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – sehe Oct 06 '17 at 11:16

0 Answers0