Michael
Michael

Reputation: 548

Exceptions when using a name of an array

I'm reading a C++ 11 book and the author says

As with any array, when we use the name of a multidimensional array, it is automatically converted to a pointer to the first element in the array.

Then the author mentions, when we are passing a name of an array to decltype it is not converted to a pointer to the first element in the array.

If we have an array like this: int arr[5][3], and then type this: decltype(arr) test. Type of test will be deduced correctly and test will be an array of 5 arrays of 3 ints.

My question is, are there any more exceptions (besides the decltype one) of the rule mentioned earlier?

Thanks.

Upvotes: 1

Views: 75

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

For example when an array is used by reference or when an array is used in some operators like sizeof operator or & operator the array is not converted to pointer to its first element.

Consider for example this program

#include <iostream>

int main() 
{
    int a[] = { 1, 2 };
    decltype( auto ) b = ( a );
}

This program will compile successfully and the array a will not be converted to a pointer.

However if to remove the parentheses around a then this program will not compile because the array a will be converted a pointer.

#include <iostream>

int main() 
{
    int a[] = { 1, 2 };
    decltype( auto ) b = a;
}

Upvotes: 1

Related Questions