Template Specialization for converting any type to string - C++ template

C++ examples for template:template function

Description

Template Specialization for converting any type to string

Demo Code

#include <iostream>
#include <sstream>
#include <iomanip>
#include <limits>

using namespace std;

template<typename T> inline string stringify(const T& input)
{
    ostringstream output;//from   www  .ja va  2s  . com
    output << input;
    return output.str();
}

template <> inline string stringify<double> (const double& input)
{
    ostringstream output;
    const int sigdigits = numeric_limits<double>::digits10;
    output << setprecision(sigdigits) << input;
    return output.str();
}

int main()
{
    cout << stringify<int>(42) << endl;
    cout << stringify<double>(45.6789012345) << endl;
    return 0;
}

Result


Related Tutorials