Is specialization of std::to_string for custom types allowed by the C++ standard?

后端 未结 4 1551
情话喂你
情话喂你 2020-12-16 09:29

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types?

namespace std {
string to_str         


        
4条回答
  •  温柔的废话
    2020-12-16 09:57

    If I'm not mistaken you can simply overload to_string for a generic type:

    template to_string(const T& _x) {
        return _x.toString();
    }
    

    and this allows use of ADL (argument dependent lookup) by your program to correctly choose the relevant to_string method based upon the type passed.

提交回复
热议问题