https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98424
Bug ID: 98424
Summary: The point of destroying temporary objects when
initializing an array
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: xmh970252187 at gmail dot com
Target Milestone: ---
#include <iostream>
struct A{
A(int id):id_(id){}
~A(){
std::cout<<"destroy A with id: "<<id_<<"\n";
}
int id_;
};
struct B{
B(A const&){
std::cout<<"construct from A\n";
}
};
int main(){
B arr[2] = {{0},{1}};
}
GCC destroy each temporary object that was bound to the parameter of `B(A
const&)` before initializing the next element.
The print is https://godbolt.org/z/j9MMqE:
````
construct from A
destroy A with id: 0
construct from A
destroy A with id: 1
````
According to
[class.temporary#6.1](https://timsong-cpp.github.io/cppwp/n4659/class.temporary#6.1).
>A temporary object bound to a reference parameter in a function call persists
>until the completion of the full-expression containing the call.
That means all temporary objects should be destroyed after the initialization
for `arr` has completed. Presumably, it is a bug of GCC that when to destroy
these temporary objects