Port changes from cppintrospection.

Notable improvements:
actually being able to do conversions from ptr to ref,
some theoretical performance improvements due to best practices
(don't call datastructure.end() every time through the loop!),
etc.