C++ Logo

std-proposals

Advanced search

Re: std::string to_string(bool value);

From: Sebastian Büttner <sebastian.buettner_at_[hidden]>
Date: Sun, 21 Jul 2019 01:48:25 +0200
Hi Jonny,

there is checking for it (unfortunately the error messages for that in
fmt are rather ugly to read cause they hide in an other error message):

https://godbolt.org/z/Hfj7EC

It can check for invalid formatting instructions (like format a string
as int :d or if you have too many placeholders in your format string).
All you need to do is to wrap the format string in a "compile time
format string" using fmt(). However you need to set the define before
the include for it to work.

I do not know the actual situation of the std::format here. Maybe it
doesn't has the feature... However there is a "std" branch in the repo
so you can check it out youself: https://github.com/fmtlib/fmt/tree/std
:)

Cheers,
Sebastian

On 2019-07-21 00:36, Jonny Grant wrote:

> Hi Sebastian
>
> You've got me converted to std::format !
>
> I like what you said that it formats by default with {} so no duplicate
> of me needing to match %s or such to particular parameters. Also I like
> that it is reversible.
>
> I imagine the compiler will be able to check the variadic parameters
> match what is expected, eg like at present for the conventional
> approach.
>
> std::string str_fmt(const char * const format, ...) __attribute__
> ((format (printf,1,2)));
>
> Jonny
>
> On 20/07/2019 23:17, Sebastian Büttner via Std-Proposals wrote: Hi
> again Jonny,
>
> just to come back to your original example and to clearify things:
> std::format is supposed to replace not only your missing
> "to_string(bool)" but to process the entire formatting in your example.
> So you example might become:
>
> std::string fmt(const std::string header, bool b)
> {
> return std::format("{} {} set", header, b);
> }
>
> Cheers,
> Sebastian
>
> On 2019-07-21 00:03, Sebastian Büttner via Std-Proposals wrote:
> Hi Jonny,
>
> std::format origins from libfmt (https://github.com/fmtlib/fmt/). The
> syntax is related to other format libraries that have prooven useful
> like pythons string.format for example.
>
> The main goal is to get the typesafety from iostreams but the
> convenient format strings of printf like approaches.
>
> Booleans are by default formatted with their textual representation
> (true|false), so the following is sufficient:
>
> std::string s = std::format("{}", true);
>
> which is kinda neat :) (you do not need type information within the
> formatting string which is great for typesafety!)
>
> Cheers,
> Sebastian
>
> On 2019-07-20 23:51, Jonny Grant via Std-Proposals wrote:
>
> Hi Garrett
>
> Many thanks for the reply with link.
>
> std::format could be good good, I hadn't heard. Currently I had always
> needed to use my own variadic std::string str_sprintf().
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0645r9.html#Syntax
>
> Saw this
> std::string s = std::format("{:s}", true);
>
> It looks like different syntax to printf % style is being used. Don't
> think that is ideal...
> But at least bool is in..
>
> Cheers, Jonny
>
> On 20/07/2019 22:24, Garrett May via Std-Proposals wrote:
>
> Hi Jonny,
>
> With respect to std::to_string, please see a reference to it here:
> https://en.cppreference.com/w/cpp/string/basic_string/to_string
>
> std::to_string appears to be defined specifically for primitive numeric
> types that match up to using sprintf and a format specifier. I don't
> think that std::to_string(bool) would be suitable, as it does not have
> a format specifier like the others. And there is also the possibility
> that when converting a bool to a string that the user genuinely want
> '0' or '1' to be produced, as they may be trying to handle the result
> as data.
>
> As Jake said, there is a format library coming out for C++20. That
> sounds like a more desirable way to handle this problem, as like I
> mentioned, choosing how one wants a bool to be represented textually is
> similar to how one would want a float or a double represented
> textually.
>
> Regards,
>
> Garrett
>
> On Sat, 20 Jul 2019, 21:39 Jonny Grant via Std-Proposals,
> <std-proposals_at_[hidden]> wrote:
> Hi Garrett
>
> Many thanks for your reply.
>
> Yes, I see std::boolalpha sets an I/O flag.. but seems a bit of a long
> way to get it into a string, I'd need to use in conjunction with
> std::istringstream to get a string...
>
> I wanted to do something like this example:
>
> IDEA
> std::string fmt(const std::string header, bool b)
> {
> return header + " " + std::to_string(b) + " set";
> }
>
> HOW I WOULD DO NOW
> As his doesn't work, I'd have it at present as:
> std::string fmt(const std::string header, bool b)
> {
> return header + " " + b?"true":"false" + " set";
> }
>
> Regards, Jonny
>
> On 20/07/2019 21:21, Garrett May via Std-Proposals wrote:
>
> std::boolalpha is already available:
>
> int main(){
> bool const a = true;
> std::cout << std::boolalpha << a << std::endl;
> }
>
> My gut instinct here is that one is supposed to control how one wants
> data to be printed out, in the same way as how float/double precision
> is done.
>
> On Sat, 20 Jul 2019, 21:16 Jonny Grant via Std-Proposals,
> <std-proposals_at_[hidden]> wrote: Hello
>
> Could std::string to_string(bool value); be added? Currently it
> outputs
> as a number due to the implicit conversion to int?
> Feel it would have been more appropriate as "true" or "false".
>
> http://www.cplusplus.com/reference/string/to_string/
>
> $ g++-8 -Wall -o to_string to_string.cpp
> $ ./to_string
> 1
>
> // g++-8 -Wall -o to_string to_string.cpp
> #include <string>
> #include <iostream>
> int main()
> {
> const bool a = true;
> const std::string b(std::to_string(a));
> std::cout << b << std::endl;
> }
>
> Cheers, Jonny
>
> .
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> http://lists.isocpp.org/mailman/listinfo.cgi/std-proposals --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> http://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2019-07-20 18:50:22