I am making a small vocabulary remembering program where words would would be flashed at me randomly for meanings. I want to use standard C++ library as Bjarne Stroustroup t
The fact is that libstdc++ actually supported std::to_string
in *-w64-mingw32 targets since 4.8.0. However, this does not include support for MinGW.org, Cygwin and variants (e.g. *-pc-msys from MSYS2). See also https://cygwin.com/ml/cygwin/2015-01/msg00245.html.
I have implemented a workaround before the bug resolved for MinGW-w64. Being different to code in other answers, this is a mimic to libstdc++ (as possible). It does not require string stream construction but depends on libstdc++ extensions. Even now I am using mingw-w64 targets on Windows, it still works well for multiple other targets (as long as long double
functions not being used).
For me, ensuring that I had:
#include <iostream>
#include<string>
using namespace std;
in my file made something like to_string(12345)
work.
Use this function...
#include<sstream>
template <typename T>
std::string to_string(T value)
{
//create an output string stream
std::ostringstream os ;
//throw the value into the string stream
os << value ;
//convert the string stream into a string and return
return os.str() ;
}
//you can also do this
//std::string output;
//os >> output; //throw whats in the string stream into the string
Here's a new-ish answer to an old thread. A new one did come up but was quickly quashed, Cygwin: g++ 5.2: ‘to_string’ is not a member of ‘std’.
Too bad, maybe we would have gotten an updated answer. According to @Alex, Cygwin g++ 5.2 is still not working as of November 3, 2015.
On January 16, 2015 Corinna Vinschen, a Cygwin maintainer at Red Hat said the problem was a shortcoming of newlib. It doesn't support most long double functions and is therefore not C99 aware.
Red Hat is,
... still hoping to get the "long double" functionality into newlib at one point.
On October 25, 2015 Corrine also said,
It would still be nice if somebody with a bit of math knowledge would contribute the missing long double functions to newlib.
So there we have it. Maybe one of us who has the knowledge, and the time, can contribute and be the hero.
Newlib is here.
#include <string>
#include <sstream>
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
#include <iostream>
int main()
{
std::cout << patch::to_string(1234) << '\n' << patch::to_string(1234.56) << '\n' ;
}
do not forget to include #include <sstream>
to_string() is only present in c++11 so if c++ version is less use some alternate methods such as sprintf or ostringstream