The following simple program segfaults on my mac (Lion) running gcc 4.2.1:
#include <iostream>
using namespace std;
struct A{
friend std::ostream& operator << (std::ostream& os, const A& a) {
os << 3 << endl;
}
};
template <typename T>
T f() { return T();}
int f() { return 2;}
int main() {
cout << f() << endl;
A a= f<A>();
cout << a << endl;
}
When I run the program, I get:
./a.out
2
3
Segmentation fault: 11
When I do a stacktrace, I get:
(gdb) run
Starting program: a.out
unable to read unknown load command 0x24
unable to read unknown load command 0x26
2
3
Program received signal SIGSEGV, Segmentation fault.
0x00007fff8b84fa49 in ?? ()
(gdb) bt
#0 0x00007fff8b84fa49 in ?? ()
#1 0x00007fff665c1ae8 in ?? ()
#2 0x0000000000000000 in ?? ()
The backtrace has no useful information (anyone know why?). This works
fine in linux.
Let’s make it clearer what happens
You forgot a
returnin youroperator<<.