c++ implementation. yuck.