In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types?
No. First of all, it is not a template function so you can't specialize it at all.
If you're asking about adding your own overload functions the answer still remains the same.
Documentation snippet from Extending the namespace std:
It is undefined behavior to add declarations or definitions to namespace std or to any namespace nested within std, with a few exceptions noted below
It is allowed to add template specializations for any standard library template to the namespace std only if the declaration depends on a user-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.
In practice everything will probably work just fine but strictly speaking the standard says there is no guarantee of what will happen.
Edit: I don't have access to the official standard so the following is from the free working draft (N4296):
17.6.4.2 Namespace use
17.6.4.2.1 Namespace std
- The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a
namespace within namespace std unless otherwise specified. A program may add a template specialization
for any standard library template to namespace std 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.181
The behavior of a C++ program is undefined if it declares
2.1 — an explicit specialization of any member function of a standard library class template, or
2.2 — an explicit specialization of any member function template of a standard library class or class template,
or
2.3 — an explicit or partial specialization of any member class template of a standard library class or class
template.
A program may explicitly instantiate a template defined in the standard library only if the declaration
depends on the name of a user-defined type and the instantiation meets the standard library requirements
for the original template.
- A translation unit shall not declare namespace std to be an inline namespace (7.3.1).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…