Try to write a c++ implementation