Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the standard to_string() functions for C++11 #4279

Merged
merged 1 commit into from
Aug 11, 2016

Conversation

Rogier-5
Copy link
Contributor

@Rogier-5 Rogier-5 commented Jul 4, 2016

If compiling according to a C++ version before C++11, then define
std::to_string ourselves.

Add a to_wstring version as well

@est31
Copy link
Contributor

est31 commented Jul 4, 2016

👍

@SmallJoker
Copy link
Member

Code looks good. What about moving the functions itos (etc) into the namespace too?

@est31
Copy link
Contributor

est31 commented Jul 4, 2016

no, we shouldn't mess with the std namespace more than we should. @Rogier-5 only added the to_string functions to std because they already exist in c++11.

@nerzhul
Copy link
Member

nerzhul commented Jul 4, 2016

Warning, there is a big difference it our implementation & stdlib implementation. std::to_string throw exception when to_string failed

@Rogier-5
Copy link
Contributor Author

Rogier-5 commented Jul 4, 2016

I'm investigating the failing travis check. The test failures are on OSX only, in test_map_settings_manager.cpp and test_settings.cpp. However, I can't seem to find the specifications of those test cases. Am I overlooking them, or are they hidden somewhere ?

@Rogier-5
Copy link
Contributor Author

Rogier-5 commented Jul 4, 2016

Warning, there is a big difference it our implementation & stdlib implementation. std::to_string throw exception when to_string failed

@nerzhul From what I read, to_string only throws when the std::string constructor throws, which is (probably) only when out of memory. ostringstream::operator<< does not throw indeed, but it sets badbit if it catches an exception itself. Presumably in particular when out-of-memory. Arguably, throwing an exception in such a case is better than failing silently ?

Or am I missing something obvious, or maybe less obvious, here ?

@est31
Copy link
Contributor

est31 commented Jul 4, 2016

Well it doesn't have to be perfect, it only has to work.

@ghost
Copy link

ghost commented Jul 14, 2016

So it seems in some implementations, std::to_string adds trailing zeros to float numbers.((float)2.4 is converted to (std::string)"2.40000")

@Rogier-5
Copy link
Contributor Author

@dlaboratory I checked the c++11 standard, and this behaviour is actually in accordance with the standard... It says that std::to_string(float) converts according to the %f format specifier for sprintf(), which in turn is documented as using a fixed format with exactly 6 decimal digits.

It seams that std::ostream::operator<<() uses the %G format instead. Thumbs up for consistency :-(

Te be continued...

@est31
Copy link
Contributor

est31 commented Jul 14, 2016

Removing my approval.

Seems this PR should be closed? We do want the %G format.

Maybe we could rename the to_string function to something different so that nobody mixes the two up.

If compiling according to a C++ version before C++11, then define
std::to_string ourselves.

Add a to_wstring version as well

As std::to_string() for floating point types uses %.6f as floating
point format converter, instead of %G, it needs special care.

To preserve ftos() behavior (which is expected to use the %G format
converter), it no longer uses to_string().
@Rogier-5
Copy link
Contributor Author

The revised patch still replaces to_string() with std::to_string(), but also ensures that ftos() uses the %G format of std::ostream::operator<<().
Still testing...

@Rogier-5
Copy link
Contributor Author

The problems with the previous version have been fixed, and all tests pass.
Please reconsider your (dis)approval.

@est31
Copy link
Contributor

est31 commented Jul 25, 2016

Ah no, overriding the behaviour of std function templates is not a good way to do things.

@Rogier-5
Copy link
Contributor Author

@est31 I don't quite understand what you mean. Are you objecting to the overriding of function templates per sé, regardless of where they are, are you objecting because they are in std::, or are you objecting because I might be overriding standard c++ functions ?

Obviously, the functions have to be in std::, because that's where the corresponding c++11 functions are. But they are not standard functions, as the code in question is not for c++11.

If you don't like the overriding of template functions at all, I can define every necessary std::to_string() function individually (not as a template) ?

@est31
Copy link
Contributor

est31 commented Jul 26, 2016

Sorry, seems I've misunderstood something. Its good. 👍

@nerzhul
Copy link
Member

nerzhul commented Jul 27, 2016

Okay for me

@est31 est31 merged commit b11720a into minetest:master Aug 11, 2016
@est31
Copy link
Contributor

est31 commented Aug 11, 2016

(taking @nerzhul 's "okay for me" as 👍 )

@Rogier-5 Rogier-5 deleted the std-to_string branch August 11, 2016 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants