https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79349
AK <hiraditya at msn dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |NEW Resolution|WORKSFORME |--- --- Comment #3 from AK <hiraditya at msn dot com> --- (In reply to AK from comment #2) > The problem is exceptions. When I compile without exceptions > (-fno-exceptions) g++ does optimize this away and gives same output as > clang. It seems clang++ compiles without exceptions by default and behaves > like g++ when -fexceptions is passed. Correction: For the example: #include<string> int main() { std::string s("abc"); return 0; } clang (with libc++) optimizes away the std::string even in the presence of exceptions (-fexceptions). When I compile without exceptions (-fno-exceptions) g++ does optimize the std::string away. However, when I introduce a call: #include<string> void foo(); int main() { std::string s("abc"); foo(); return 0; } clang++ still optimizes the std::string but g++ does not. I think the problem is with libstdc++ because when clang is using libstdc++ I can see the destructor. Sorry for the confusion.