C++ version of paste.py