Why does clang++ destroy only one foo object?

C++Clang

C++ Problem Overview


I have the following example code:

#include <iostream>
using namespace std;

struct foo {
    foo()  { cout << "foo constructed.\n"; }
    ~foo() { cout << "foo destroyed.\n"; }
};

struct bar {
    bar(foo t=foo{}) { }
};

int main(int argc, char **argv) {
    bar X[2]{};
    return 0;
}

When I compile it with clang++ -std=c++11 test.cc, the program produces the following output:

foo constructed.
foo constructed.
foo destroyed.

but I expected an additional "foo destroyed." between the two "foo constructed." lines. Why is only one foo destroyed? This happens with clang 3.5.1 as well as 3.6.0.

C++ Solutions


Solution 1 - C++

Thanks for all the people who tested it! This seems to be a bug in clang. I'd appreciate if someone reports it to llvm.org. My few bugs reports there were, let say, not really helpful, so I am not looking to repeat that experience.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwonderingnewbieView Question on Stackoverflow
Solution 1 - C++wonderingnewbieView Answer on Stackoverflow