using cpp template to implement algorithm