I can't really say whether or not your code is in a state of grace, but I can lay out some of the issues by which to decide that. That is, you pass in an object of unknown type and you want to do something appropriate with it that depends on the full type. The alternative run-time polymophism way to do this is to use virtual functions. Virtual function are faster. The amount of time it takes to resolve the type does not scale up with the number of possible classes.
Virtual functions are extensible.
Given a library that uses virtual functions you can always derive a new class that defines a new foo and it works. If the function above was compiled into a library you could not add a new kind of matrix to it. That generally means you can add functionality to your library, but I can't. I don't know a good extensible external run-time polymorphism technique, but it is possible there is a trick for it. This is basically a letter-envelope pattern with templates.