Question: Is it correct that GCC's implementation destroys a std::initializer_list array returned from a function at the end of the return full-expression?
Answer: No, this is not correct.
According to the C 11 standard, a return statement with a braced-init-list initializes the object or reference to be returned by copy-list-initialization from the specified initializer list. When an initializer list is used to create an initializer_list object, an array is allocated to store the elements of the list. The lifetime of the array is defined as follows:
The lifetime of the array is the same as that of the initializer_list object.
In the example provided, the initializer_list object is created as a return value from the send() function. It is initialized with three noisydt objects and then returned to the receive() function. The lifetime of the array associated with the initializer_list object should extend into the receive() function, allowing the elements of the list to be accessed and used.
However, the GCC implementation incorrectly destroys the array before the return value can be used, resulting in undefined behavior. The correct behavior is for the array to persist until the end of the receive() function. Both Clang and ICC demonstrate the correct behavior.
The GCC implementation's handling of std::initializer_list return values is not in accordance with the C 11 standard. Clang and ICC provide correct implementations, and it is recommended to use these compilers when working with std::initializer_lists.
The above is the detailed content of Does GCC Destroy `std::initializer_list` Arrays Returned from Functions Before the End of the Return Full-Expression?. For more information, please follow other related articles on the PHP Chinese website!