#include <iostream>

#include <execinfo.h>

namespace stck {


class stacktrace_t {};

stacktrace_t stacktrace;

std::ostream &operator<<(std::ostream &out, stacktrace_t) {
  out << "stacktrace:\n";
  
  size_t max = 256;
  void **stackframes = new void *[max];
  size_t numel;
  while ((numel = backtrace(stackframes, max)) >= max) {
    max *= 2;
    delete[] stackframes;
    stackframes = new void *[max];
  }
  
  char **symbols = backtrace_symbols(stackframes, numel);
  for(size_t i = 0; i < numel; ++i)
    out << symbols[i] << '\n';
  
  delete[] stackframes;
  return out;
}


}

int main() {
	std::cout << stck::stacktrace;
	
}