Stefan Ram wrote:
>>> So, as the array is temporary, a pointer to its element can and will
>>> become dangling as far as I can see.
>> I thought the clarification (making the return value a constexpr) takes
>
> »constexpr« did not occur so far. A const& can extend the
> lifetime of a temporary, but it must be bound to it directly,
> not via a pointer.
>
> »::std::begin« does not accept an initialization list, only
> an array reference, so a temporary array will be created and
> the begin pointer of that array will be computed. The result
> is a pointer to the begin of that array. Then, that array will
> be destroyed. It is as if
18.9.3 Initializer list range access
template<class E> const E* begin(initializer_list<E> il) noexcept;
1 Returns: il.begin().
> #include <initializer_list>
> #include <iostream>
> #include <ostream>
>
> struct array
> { array( ::std::initializer_list< int >const ){};
> ~array(){ ::std::cout << "I, temporary array, destroyed\n"; } };
>
> int * begin( array ){ return nullptr; }
So this always returns nullptr.
> int main()
> { ::std::cout << "before\n";
> const int * y = begin( { 1, 2, 3 }); /* this is like your code */
So this is equivalent to y = nullptr;
> ::std::cout << "after\n";
> ::std::cout << "y = " << y << '\n'; }
>
> before
> I, temporary array, destroyed
> after
> y = 0
int main()
{
std::cout << "before\n";
const int * y = std::begin( { 1, 2, 3 }); /* this is like your code */
std::cout << "after\n";
std::cout << "y = " << y << '\n';
}
before
after
y = 0x8051294
--
Ian Collins