Generalized pattern matching in c++