Googleで以下のコードを見つけました。'%。* f'がCタイプの形式の文字列で行うような精度を示す方法を提供しないことを除いて、それは私がやりたいことをほぼ実行します。また、小数点以下5桁を超えるものはありません。C文字列とsnprintfに固執する必要がありますか?
#include <string>
#include <sstream>
#include <iostream>
template <class T>
std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
{
std::ostringstream oss;
oss << f << t;
return oss.str();
}
int main()
{
std::cout<<to_string<double>(3.1415926535897931, std::dec)<<std::endl;
return 0;
}