Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does clang++ destroy only one foo object?

Tags:

c++

clang

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.

like image 970
wonderingnewbie Avatar asked Mar 11 '15 15:03

wonderingnewbie


People also ask

Does clang define __ GNUC __?

(GNU C is a language, GCC is a compiler for that language.Clang defines __GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ according to the version of gcc that it claims full compatibility with.

What is clang option?

The Clang Compiler is an open-source compiler for the C family of programming languages, aiming to be the best in class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for many targets.

What is clang EXE?

Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA, and HIP frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.

How does clang work?

Clang Design: Like many other compilers design, Clang compiler has three phase: The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code. The optimizer: its goal is to do some optimization on the AST generated by the front end.


1 Answers

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.

like image 167
wonderingnewbie Avatar answered Sep 18 '22 16:09

wonderingnewbie