/* $ cat /proc/version Linux version 2.6.20-15-generic (root@yellow) (gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)) #2 SMP Sun Apr 15 06:17:24 UTC 2007 $ gcc --version gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ g++ simplestack.cpp -o simplestack -g simplestack.cpp: In member function ‘void SimpleStack::pop() [with T = int, long unsigned int N = 4ul]’: simplestack.cpp:52: instantiated from here simplestack.cpp:32: internal compiler error: Segmentation fault Please submit a full bug report, with preprocessed source if appropriate. preprocessed source: http://cpp.sourceforge.net/?show=36942 */ #include #include template class SimpleStack { public: SimpleStack() : ptr(reinterpret_cast(&buffer[0])) { } void push(const T& element) { if(num >= N) throw std::out_of_range("stack full"); new (ptr + num) T(element); ++num; } bool empty() const { return num <= 0; } T& top() { return *(ptr + num - 1); } void pop() { top.~T(); // this is the line causing the error! --num; } private: SimpleStack(const SimpleStack&); SimpleStack& operator=(const SimpleStack&); char buffer[N * sizeof(T)]; T *ptr; std::size_t num; }; #include int main() { SimpleStack stack; stack.push(3); std::cout << stack.top() << std::endl; stack.pop(); } -- Summary: Compiler segmentation fault when trying to call x.~X(); (X &x = f();) in a template Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: rsalmin2 at cc dot hut dot fi GCC build triplet: x86_64-linux-gnu GCC host triplet: x86_64-linux-gnu GCC target triplet: x86_64-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32241