This is because of the member function:
string& operator=( char ch );
There is an implicit conversion from floating point to integer types in C ++ ( char is an integer type).
You can usually use -Wfloat-conversion in g ++ to get a warning about this conversion, but I tried it and it did not warn. (Maybe a compiler error?)
An easy way to change the code to get errors for unexpected floating point / integer conversions is:
s = { 1.0f };
Another option is to make the string function as the return type (generally speaking, this construction is preferable to have the "out" reference parameter):
static string get_text() { return 1.0f; }
Unfortunately, this is one of the many small errors associated with using std::string that was "designed" when C ++ was still very young, and it was not clear what undesirable long-term consequences would arise.
source share