Yes, add the manipulator(s) you desire to the function signature and forward them to the stream.
template<typename T, typename Manip>
std::string Stringify(T const &value, Manip manip)
{
std::stringstream ss;
ss << manip << value;
return ss.str();
}
With the sample code;
int main()
{
using namespace std;
// The precision here is set to be sufficient to print the test platform
cout << Stringify(numeric_limits<float>::max(), setprecision(50)) << endl;
}
I assume that more than one manipulator will be used. To this end, function overloads can be added for the desired number of manipulators, or you can use (with C++11) variadic templates and perfect forwarding.
template <typename Stream>
Stream& AddManip(Stream& str)
{
// end the recursion
return str;
}
template <typename Stream, typename Head, typename... Tails>
Stream& AddManip(Stream& str, Head&& head, Tails&&... tails)
{
// add the head manipulator and forward the rest
str << std::forward<Head>(head);
return AddManip(str, std::forward<Tails>(tails)...);
}
template<typename T, typename... Manip>
std::string Stringify(T const &value, Manip&&... manip)
{
std::stringstream ss;
// add the manipulators to the stream
AddManip(ss, std::forward<Manip>(manip)...);
ss << value;
return ss.str();
}
int main()
{
using namespace std;
cout << Stringify(numeric_limits<int>::max(), setprecision(40), std::hex) << endl;
}