mercredi 31 août 2016

understand implementation of std::to_string

c++11 implements std::to_string, I took a look at the implementation. It calls vsnprintf internally. Okay, but why does it always set the size parameter as 4 times size of the type?

 inline string
 to_string(int __val)
 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
                                          "%d", __val); }

 inline string
 to_string(unsigned __val)
 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
                                          4 * sizeof(unsigned),
                                          "%u", __val); }

 inline string
 to_string(long __val)
 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
                                          "%ld", __val); }

Aucun commentaire:

Enregistrer un commentaire