It’s not safe, because it’s not legal (§17.6.4.2.1):
The behavior of a C++ program is undefined if it adds declarations or definitions to namespace
std
or to a namespace within namespacestd
unless otherwise specified. A program may add a template specialization for any standard library template to namespacestd
only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
So you may add specialisations for your own types. You may not add overloads (or indeed anything else).
Your current code is the correct way of doing this.