Auto keyword deduced to a pointer or array decay?

83 Views Asked by At

I'm following the tutorial from https://www.learncpp.com/cpp-tutorial/introduction-to-iterators/

Example:

#include <array>
#include <iostream>

int main()
{
    std::array data{ 0, 1, 2, 3, 4, 5, 6 };

    auto begin{ &data[0] };
    // note that this points to one spot beyond the last element
    auto end{ begin + std::size(data) };

    // for-loop with pointer
    for (auto ptr{ begin }; ptr != end; ++ptr) // ++ to move to next element
    {
        std::cout << *ptr << ' '; // Indirection to get value of current element
    }
    std::cout << '\n';

    return 0;
}

I'm trying to understand this initialization auto begin{ &data[0] };. So a pointer wasn't explicitly defined (auto* begin{ &data[0] };)

Did the auto keyword deduced it to a pointer or was it because of array decay? If the latter is the reason, I thought array decay only happened with C-style arrays.

Thanks for the help

0

There are 0 best solutions below