Topic: Adding more to_string overloads for containers
Author: John McFarlane <john@mcfarlane.name>
Date: Mon, 27 Aug 2018 10:48:28 +0100
Raw View
--0000000000004e9666057467a10d
Content-Type: text/plain; charset="UTF-8"
On Sun, 26 Aug 2018 at 14:35 <ericcurtin17@gmail.com> wrote:
> Would be particularly helpful, when you want to put all of the contents of
> a container in a std::string without all the boilerplate. Some example test
> code;
>
> #include <iostream>
> #include <map>
> #include <string>
> #include <vector>
> #include <set>
>
> using namespace std;
>
> string to_string(const string& val) {
> return val;
> }
>
> string to_string(const char val) {
> return string(1, val);
> }
>
> template <typename T>
> string to_string(const vector<T>& val) {
> string ret = "{";
> for (const auto& v : val) {
> ret += to_string(v) + ", ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> template <typename T>
> string to_string(const set<T>& val) {
> string ret = "{";
> for (const auto& v : val) {
> ret += to_string(v) + ", ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> template <typename k, typename v>
> string to_string(const map<k, v>& val) {
> string ret = "{";
> for (const auto& kv : val) {
> ret += '{' + to_string(kv.first) + ", " + to_string(kv.second) + "}, ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> int main() {
> string a = "asdasd";
> cout << to_string(a) << '\n';
>
> vector<string> b = {"djassda", "dsajkh"};
> cout << to_string(b) << '\n';
>
> vector<int> c = {1, 2};
> cout << to_string(c) << '\n';
>
> vector<char> d = {'1', 'd'};
> cout << to_string(d) << '\n';
>
> map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
> cout << to_string(m) << '\n';
>
> set<string> s = {"John", "Kelly", "Amanda", "Kim"};
> cout << to_string(s) << '\n';
> }
>
> This was touched upon recently in a post about customization points by
Arthur O'Dwyer:
https://quuxplusone.github.io/blog/2018/08/23/customization-point-or-named-function-pick-one/
Some of these overloads are more contentious than others. For
`std::string`, you could easily argue for this (but see below). The `char`
types start to elicit harder design questions, e.g. should the output be a
character or a number?
For containers, there is even more room for interpretation about what is
the 'right' (canonical?) way to print an object. Some questions might
include:
- what is the right delimiter to use?
- what if the contents themselves are containers?
- should there be some attempt to make the output readable again so it can
be used for serialization?
- should the sequence really be wrapped in `{` and `}`? And if so, why?
- Are we trying to reflect code we might use to initialize such an object?
And if so, shouldn't we wrap `std::string` in quotes, `"`?
(Note that I'm not necessarily interested in discussing these!)
I'm not sure there is one version, for example, of `to_string(vector<T>)`
which would win out over all others -- even though yours is perfectly
acceptable and frequently implemented.
What is more useful is a good way to let users provide their own answers to
those questions which best suit their application. `to_string` facilitates
a method of doing that. Perhaps we can do better.
But for `std::string` at least, this seems reasonable to me.
Thanks,
John
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnT0n4CxWP%3DXuvuxiSdMgKn-z_BUsZXdXGCYtHLKNGVphw%40mail.gmail.com.
--0000000000004e9666057467a10d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, 26 Aug=
2018 at 14:35 <<a href=3D"mailto:ericcurtin17@gmail.com">ericcurtin17@g=
mail.com</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D=
"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D=
"ltr"><div>Would be particularly helpful, when you want to put all of the c=
ontents of a container in a std::string without all the boilerplate. Some e=
xample test code;</div><div><br></div><div>#include <iostream></div><=
div>#include <map></div><div>#include <string></div><div>#inclu=
de <vector></div><div>#include <set></div><div><br></div><div>u=
sing namespace std;</div><div><br></div><div>string to_string(const string&=
amp; val) {</div><div>=C2=A0 return val;</div><div>}</div><div><br></div><d=
iv>string to_string(const char val) {</div><div>=C2=A0 return string(1, val=
);</div><div>}</div><div><br></div><div>template <typename T></div><d=
iv>string to_string(const vector<T>& val) {</div><div>=C2=A0 stri=
ng ret =3D "{";</div><div>=C2=A0 for (const auto& v : val) {<=
/div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + ", ";</div><div>=
=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret.substr(0, ret.size() -=
2);</div><div>=C2=A0 ret +=3D "}";</div><div><br></div><div>=C2=
=A0 return ret;</div><div>}</div><div><br></div><div>template <typename =
T></div><div>string to_string(const set<T>& val) {</div><div>=
=C2=A0 string ret =3D "{";</div><div>=C2=A0 for (const auto& =
v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + ", ";<=
/div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret.substr(0, re=
t.size() - 2);</div><div>=C2=A0 ret +=3D "}";</div><div><br></div=
><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>template <=
typename k, typename v></div><div>string to_string(const map<k, v>=
& val) {</div><div>=C2=A0 string ret =3D "{";</div><div>=C2=
=A0 for (const auto& kv : val) {</div><div>=C2=A0 =C2=A0 ret +=3D '=
{' + to_string(kv.first) + ", " + to_string(kv.second) + &quo=
t;}, ";</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret=
..substr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D "}";</div><=
div><br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>=
int main() {</div><div>=C2=A0 string a =3D "asdasd";</div><div>=
=C2=A0 cout << to_string(a) << '\n';</div><div><br></di=
v><div>=C2=A0 vector<string> b =3D {"djassda", "dsajkh=
"};</div><div>=C2=A0 cout << to_string(b) << '\n';=
</div><div><br></div><div>=C2=A0 vector<int> c =3D {1, 2};</div><div>=
=C2=A0 cout << to_string(c) << '\n';</div><div><br></di=
v><div>=C2=A0 vector<char> d =3D {'1', 'd'};</div><di=
v>=C2=A0 cout << to_string(d) << '\n';</div><div><br></=
div><div>=C2=A0 map<int, char> m =3D {{1, 'a'}, {3, 'b=
9;}, {5, 'c'}, {7, 'd'}};</div><div>=C2=A0 cout << to=
_string(m) << '\n';</div><div><br></div><div>=C2=A0 set<st=
ring> s =3D {"John", "Kelly", "Amanda", &q=
uot;Kim"};</div><div>=C2=A0 cout << to_string(s) << '\=
n';</div><div>}</div><div><br></div></div></blockquote><div>This was to=
uched upon recently in a post about customization points by Arthur O'Dw=
yer:</div><div><a href=3D"https://quuxplusone.github.io/blog/2018/08/23/cus=
tomization-point-or-named-function-pick-one/">https://quuxplusone.github.io=
/blog/2018/08/23/customization-point-or-named-function-pick-one/</a></div><=
div><br></div><div>Some of these overloads are more contentious than others=
.. For `std::string`, you could easily argue for this (but see below). The `=
char` types start to elicit harder design questions, e.g. should the output=
be a character or a number?</div><div><br></div><div>For containers, there=
is even more room for interpretation about what is the 'right' (ca=
nonical?) way to print an object. Some questions might include:</div><div>-=
what is the right delimiter to use?</div><div>- what if the contents thems=
elves are containers?</div><div>- should there be some attempt to make the =
output readable again so it can be used for serialization?</div><div>- shou=
ld the sequence really be wrapped in `{` and `}`? And if so, why?=C2=A0</di=
v><div>- Are we trying to reflect code we might use to initialize such an o=
bject? And if so, shouldn't we wrap `std::string` in quotes, `"`?<=
/div><div><br></div><div>(Note that I'm not necessarily interested in d=
iscussing these!)</div><div><br></div><div>I'm not sure there is one ve=
rsion, for example, of `to_string(vector<T>)` which would win out ove=
r all others -- even though yours is perfectly acceptable and frequently im=
plemented.</div><div><br></div><div>What is more useful is a good way to le=
t users provide their own answers to those questions which best suit their =
application. `to_string` facilitates a method of doing that. Perhaps we can=
do better.</div><div><br></div><div>But for `std::string` at least, this s=
eems reasonable to me.</div><div><br></div><div>Thanks,</div><div>John</div=
></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CABPJVnT0n4CxWP%3DXuvuxiSdMgKn-z_BUsZ=
XdXGCYtHLKNGVphw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnT0n4CxWP=
%3DXuvuxiSdMgKn-z_BUsZXdXGCYtHLKNGVphw%40mail.gmail.com</a>.<br />
--0000000000004e9666057467a10d--
.
Author: j c <james.a.cooper@gmail.com>
Date: Mon, 27 Aug 2018 10:50:00 +0100
Raw View
--0000000000001ab3cb057467a645
Content-Type: text/plain; charset="UTF-8"
What if I don't want to separate the values with a comma?
Why does your to_string(const std::string&) overload return a copy and
isn't just a nop?
On Sunday, August 26, 2018, <ericcurtin17@gmail.com> wrote:
> Would be particularly helpful, when you want to put all of the contents of
> a container in a std::string without all the boilerplate. Some example test
> code;
>
> #include <iostream>
> #include <map>
> #include <string>
> #include <vector>
> #include <set>
>
> using namespace std;
>
> string to_string(const string& val) {
> return val;
> }
>
> string to_string(const char val) {
> return string(1, val);
> }
>
> template <typename T>
> string to_string(const vector<T>& val) {
> string ret = "{";
> for (const auto& v : val) {
> ret += to_string(v) + ", ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> template <typename T>
> string to_string(const set<T>& val) {
> string ret = "{";
> for (const auto& v : val) {
> ret += to_string(v) + ", ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> template <typename k, typename v>
> string to_string(const map<k, v>& val) {
> string ret = "{";
> for (const auto& kv : val) {
> ret += '{' + to_string(kv.first) + ", " + to_string(kv.second) + "}, ";
> }
>
> ret = ret.substr(0, ret.size() - 2);
> ret += "}";
>
> return ret;
> }
>
> int main() {
> string a = "asdasd";
> cout << to_string(a) << '\n';
>
> vector<string> b = {"djassda", "dsajkh"};
> cout << to_string(b) << '\n';
>
> vector<int> c = {1, 2};
> cout << to_string(c) << '\n';
>
> vector<char> d = {'1', 'd'};
> cout << to_string(d) << '\n';
>
> map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
> cout << to_string(m) << '\n';
>
> set<string> s = {"John", "Kelly", "Amanda", "Kim"};
> cout << to_string(s) << '\n';
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/
> isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-
> b371-3fca7c43c6e8%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAFQaeCBkH1K12dyzCJhGbVTDCKL3CUCmZ8iXTk2O3_E6DGT%2BOA%40mail.gmail.com.
--0000000000001ab3cb057467a645
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
What if I don't want to separate the values with a comma?<div><br></div=
><div>Why does your to_string(const std::string&) overload return a cop=
y and isn't just a nop?<br><br>On Sunday, August 26, 2018, <<a href=
=3D"mailto:ericcurtin17@gmail.com">ericcurtin17@gmail.com</a>> wrote:<br=
><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1=
px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Would be particularly=
helpful, when you want to put all of the contents of a container in a std:=
:string without all the boilerplate. Some example test code;</div><div><br>=
</div><div>#include <iostream></div><div>#include <map></div><d=
iv>#include <string></div><div>#include <vector></div><div>#inc=
lude <set></div><div><br></div><div>using namespace std;</div><div><b=
r></div><div>string to_string(const string& val) {</div><div>=C2=A0 ret=
urn val;</div><div>}</div><div><br></div><div>string to_string(const char v=
al) {</div><div>=C2=A0 return string(1, val);</div><div>}</div><div><br></d=
iv><div>template <typename T></div><div>string to_string(const vector=
<T>& val) {</div><div>=C2=A0 string ret =3D "{";</div><=
div>=C2=A0 for (const auto& v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D=
to_string(v) + ", ";</div><div>=C2=A0 }</div><div><br></div><div=
>=C2=A0 ret =3D ret.substr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D &q=
uot;}";</div><div><br></div><div>=C2=A0 return ret;</div><div>}</div><=
div><br></div><div>template <typename T></div><div>string to_string(c=
onst set<T>& val) {</div><div>=C2=A0 string ret =3D "{"=
;</div><div>=C2=A0 for (const auto& v : val) {</div><div>=C2=A0 =C2=A0 =
ret +=3D to_string(v) + ", ";</div><div>=C2=A0 }</div><div><br></=
div><div>=C2=A0 ret =3D ret.substr(0, ret.size() - 2);</div><div>=C2=A0 ret=
+=3D "}";</div><div><br></div><div>=C2=A0 return ret;</div><div>=
}</div><div><br></div><div>template <typename k, typename v></div><di=
v>string to_string(const map<k, v>& val) {</div><div>=C2=A0 strin=
g ret =3D "{";</div><div>=C2=A0 for (const auto& kv : val) {<=
/div><div>=C2=A0 =C2=A0 ret +=3D '{' + to_string(kv.first) + "=
, " + to_string(kv.second) + "}, ";</div><div>=C2=A0 }</div>=
<div><br></div><div>=C2=A0 ret =3D ret.substr(0, ret.size() - 2);</div><div=
>=C2=A0 ret +=3D "}";</div><div><br></div><div>=C2=A0 return ret;=
</div><div>}</div><div><br></div><div>int main() {</div><div>=C2=A0 string =
a =3D "asdasd";</div><div>=C2=A0 cout << to_string(a) <&=
lt; '\n';</div><div><br></div><div>=C2=A0 vector<string> b =
=3D {"djassda", "dsajkh"};</div><div>=C2=A0 cout <&l=
t; to_string(b) << '\n';</div><div><br></div><div>=C2=A0 vect=
or<int> c =3D {1, 2};</div><div>=C2=A0 cout << to_string(c) <=
;< '\n';</div><div><br></div><div>=C2=A0 vector<char> d =
=3D {'1', 'd'};</div><div>=C2=A0 cout << to_string(d)=
<< '\n';</div><div><br></div><div>=C2=A0 map<int, char>=
; m =3D {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd&#=
39;}};</div><div>=C2=A0 cout << to_string(m) << '\n';</=
div><div><br></div><div>=C2=A0 set<string> s =3D {"John", &=
quot;Kelly", "Amanda", "Kim"};</div><div>=C2=A0 co=
ut << to_string(s) << '\n';</div><div>}</div><div><br><=
/div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/7636=
2bfa-a887-4429-<wbr>b371-3fca7c43c6e8%40isocpp.org</a><wbr>.<br>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAFQaeCBkH1K12dyzCJhGbVTDCKL3CUCmZ8iX=
Tk2O3_E6DGT%2BOA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAFQaeCBkH1K12d=
yzCJhGbVTDCKL3CUCmZ8iXTk2O3_E6DGT%2BOA%40mail.gmail.com</a>.<br />
--0000000000001ab3cb057467a645--
.
Author: ericcurtin1990@gmail.com
Date: Mon, 27 Aug 2018 03:15:54 -0700 (PDT)
Raw View
------=_Part_895_451579772.1535364954932
Content-Type: multipart/alternative;
boundary="----=_Part_896_339646981.1535364954933"
------=_Part_896_339646981.1535364954933
Content-Type: text/plain; charset="UTF-8"
For the following questions:
what is the right delimiter to use?
should the sequence really be wrapped in `{` and `}`? And if so, why?
I used a format most similar to how you would initialise a container in
C++11. The main reason being consistency.
std::map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
So commas to separate the values and curly braces to wrap the sequences.
what if the contents themselves are containers?
Using my technique, you will recursively call to_string, until all the
containers are represented as strings. Of course, we would have to think
more about custom classes and structs. Have a couple of ideas in my head
for that, hopefully you guys do too!
should there be some attempt to make the output readable again so it can be
used for serialization?
Are we trying to reflect code we might use to initialize such an object?
And if so, shouldn't we wrap `std::string` in quotes, `"`?
Good question, maybe it's worth considering wrapping chars in single quotes
and strings in double quotes. Although, we don't really make any attempt to
make the existing std::to_string functions serializable (if that's even a
word). Maybe it's worth considering for collections, if we added stovector,
stomap type functions. Although recursively trying to parse a collection of
collections mightn't be easily achievable.
Why does your to_string(const std::string&) overload return a copy and
isn't just a nop?
This is true, it is a no-op. This is for generic programming, for example
an "std::string to_string(const std::vector<T>& val)" call could as easily
be std::vector<std::string> or std::vector<int>, etc.
I have a better C++98 example here (the codebase I work on is C++98
unfortunately), I could create a c++17 example or whatever for the sake of
discussing in this forum:
https://github.com/ericcurtin/to_stringxx/blob/master/to_string.hpp
My example posted here didn't take into account if you had an empty
container.
On Monday, August 27, 2018 at 10:50:02 AM UTC+1, j c wrote:
>
> What if I don't want to separate the values with a comma?
>
> Why does your to_string(const std::string&) overload return a copy and
> isn't just a nop?
>
> On Sunday, August 26, 2018, <ericcu...@gmail.com <javascript:>> wrote:
>
>> Would be particularly helpful, when you want to put all of the contents
>> of a container in a std::string without all the boilerplate. Some example
>> test code;
>>
>> #include <iostream>
>> #include <map>
>> #include <string>
>> #include <vector>
>> #include <set>
>>
>> using namespace std;
>>
>> string to_string(const string& val) {
>> return val;
>> }
>>
>> string to_string(const char val) {
>> return string(1, val);
>> }
>>
>> template <typename T>
>> string to_string(const vector<T>& val) {
>> string ret = "{";
>> for (const auto& v : val) {
>> ret += to_string(v) + ", ";
>> }
>>
>> ret = ret.substr(0, ret.size() - 2);
>> ret += "}";
>>
>> return ret;
>> }
>>
>> template <typename T>
>> string to_string(const set<T>& val) {
>> string ret = "{";
>> for (const auto& v : val) {
>> ret += to_string(v) + ", ";
>> }
>>
>> ret = ret.substr(0, ret.size() - 2);
>> ret += "}";
>>
>> return ret;
>> }
>>
>> template <typename k, typename v>
>> string to_string(const map<k, v>& val) {
>> string ret = "{";
>> for (const auto& kv : val) {
>> ret += '{' + to_string(kv.first) + ", " + to_string(kv.second) + "},
>> ";
>> }
>>
>> ret = ret.substr(0, ret.size() - 2);
>> ret += "}";
>>
>> return ret;
>> }
>>
>> int main() {
>> string a = "asdasd";
>> cout << to_string(a) << '\n';
>>
>> vector<string> b = {"djassda", "dsajkh"};
>> cout << to_string(b) << '\n';
>>
>> vector<int> c = {1, 2};
>> cout << to_string(c) << '\n';
>>
>> vector<char> d = {'1', 'd'};
>> cout << to_string(d) << '\n';
>>
>> map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
>> cout << to_string(m) << '\n';
>>
>> set<string> s = {"John", "Kelly", "Amanda", "Kim"};
>> cout << to_string(s) << '\n';
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to std-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%40isocpp.org?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/50fd77b0-d626-409b-b4de-6f05f3489e73%40isocpp.org.
------=_Part_896_339646981.1535364954933
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>For the following questions:</div><div><br></div><div=
>what is the right delimiter to use?</div><div>should the sequence really b=
e wrapped in `{` and `}`? And if so, why?=C2=A0</div><div><br></div><div>I =
used a format most similar to how you would initialise a container in C++11=
.. The main reason being consistency.</div><div><br></div><div>std::map<i=
nt, char> m =3D {{1, 'a'}, {3, 'b'}, {5, 'c'}, {=
7, 'd'}};</div><div><br></div><div>So commas to separate the values=
and curly braces to wrap the sequences.</div><div><br></div><div>what if t=
he contents themselves are containers?</div><div><br></div><div>Using my te=
chnique, you will recursively call to_string, until all the containers are =
represented as strings. Of course, we would have to think more about custom=
classes and structs. Have a couple of ideas in my head for that, hopefully=
you guys do too!</div><div><br></div><div>should there be some attempt to =
make the output readable again so it can be used for serialization?<br></di=
v><div>Are we trying to reflect code we might use to initialize such an obj=
ect? And if so, shouldn't we wrap `std::string` in quotes, `"`?<br=
></div><div><br></div><div>Good question, maybe it's worth considering =
wrapping chars in single quotes and strings in double quotes. Although, we =
don't really make any attempt to make the existing std::to_string funct=
ions serializable (if that's even a word). Maybe it's worth conside=
ring for collections, if we added stovector, stomap type functions. Althoug=
h recursively trying to parse a collection of collections mightn't be e=
asily achievable.</div><div><br></div><div>Why does your to_string(const st=
d::string&) overload return a copy and isn't just a nop?<br></div><=
div><br></div><div>This is true, it is a no-op. This is for generic program=
ming, for example an "std::string to_string(const std::vector<T>=
& val)" call could as easily be std::vector<std::string> or =
std::vector<int>, etc.</div><div><br></div><div>I have a better C++98=
example here (the codebase I work on is C++98 unfortunately), I could crea=
te a c++17 example or whatever for the sake of discussing in this forum:</d=
iv><div><br></div><div>https://github.com/ericcurtin/to_stringxx/blob/maste=
r/to_string.hpp<br></div><div><br></div><div>My example posted here didn=
9;t take into account if you had an empty container.</div><div><br></div>On=
Monday, August 27, 2018 at 10:50:02 AM UTC+1, j c wrote:<blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;">What if I don't want to separate the values=
with a comma?<div><br></div><div>Why does your to_string(const std::string=
&) overload return a copy and isn't just a nop?<br><br>On Sunday, A=
ugust 26, 2018, <<a href=3D"javascript:" target=3D"_blank" gdf-obfuscat=
ed-mailto=3D"RN0sULroAQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'=
;javascript:';return true;" onclick=3D"this.href=3D'javascript:'=
;;return true;">ericcu...@gmail.com</a>> wrote:<br><blockquote class=3D"=
gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-=
left:1ex"><div dir=3D"ltr"><div>Would be particularly helpful, when you wan=
t to put all of the contents of a container in a std::string without all th=
e boilerplate. Some example test code;</div><div><br></div><div>#include &l=
t;iostream></div><div>#include <map></div><div>#include <string=
></div><div>#include <vector></div><div>#include <set></div>=
<div><br></div><div>using namespace std;</div><div><br></div><div>string to=
_string(const string& val) {</div><div>=C2=A0 return val;</div><div>}</=
div><div><br></div><div>string to_string(const char val) {</div><div>=C2=A0=
return string(1, val);</div><div>}</div><div><br></div><div>template <t=
ypename T></div><div>string to_string(const vector<T>& val) {<=
/div><div>=C2=A0 string ret =3D "{";</div><div>=C2=A0 for (const =
auto& v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) + "=
, ";</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =3D ret.su=
bstr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D "}";</div><div=
><br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></div><div>tem=
plate <typename T></div><div>string to_string(const set<T>&=
val) {</div><div>=C2=A0 string ret =3D "{";</div><div>=C2=A0 for=
(const auto& v : val) {</div><div>=C2=A0 =C2=A0 ret +=3D to_string(v) =
+ ", ";</div><div>=C2=A0 }</div><div><br></div><div>=C2=A0 ret =
=3D ret.substr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D "}";=
</div><div><br></div><div>=C2=A0 return ret;</div><div>}</div><div><br></di=
v><div>template <typename k, typename v></div><div>string to_string(c=
onst map<k, v>& val) {</div><div>=C2=A0 string ret =3D "{&qu=
ot;;</div><div>=C2=A0 for (const auto& kv : val) {</div><div>=C2=A0 =C2=
=A0 ret +=3D '{' + to_string(kv.first) + ", " + to_string=
(kv.second) + "}, ";</div><div>=C2=A0 }</div><div><br></div><div>=
=C2=A0 ret =3D ret.substr(0, ret.size() - 2);</div><div>=C2=A0 ret +=3D &qu=
ot;}";</div><div><br></div><div>=C2=A0 return ret;</div><div>}</div><d=
iv><br></div><div>int main() {</div><div>=C2=A0 string a =3D "asdasd&q=
uot;;</div><div>=C2=A0 cout << to_string(a) << '\n';</d=
iv><div><br></div><div>=C2=A0 vector<string> b =3D {"djassda&quo=
t;, "dsajkh"};</div><div>=C2=A0 cout << to_string(b) <&l=
t; '\n';</div><div><br></div><div>=C2=A0 vector<int> c =3D {1=
, 2};</div><div>=C2=A0 cout << to_string(c) << '\n';</d=
iv><div><br></div><div>=C2=A0 vector<char> d =3D {'1', 'd=
'};</div><div>=C2=A0 cout << to_string(d) << '\n';<=
/div><div><br></div><div>=C2=A0 map<int, char> m =3D {{1, 'a'=
}, {3, 'b'}, {5, 'c'}, {7, 'd'}};</div><div>=C2=A0 =
cout << to_string(m) << '\n';</div><div><br></div><div>=
=C2=A0 set<string> s =3D {"John", "Kelly", "=
Amanda", "Kim"};</div><div>=C2=A0 cout << to_string(s)=
<< '\n';</div><div>}</div><div><br></div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
RN0sULroAQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:&=
#39;;return true;" onclick=3D"this.href=3D'javascript:';return true=
;">std-proposal...@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"javascript:" target=3D"_bla=
nk" gdf-obfuscated-mailto=3D"RN0sULroAQAJ" rel=3D"nofollow" onmousedown=3D"=
this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'=
;javascript:';return true;">std-pr...@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank" =
rel=3D"nofollow" onmousedown=3D"this.href=3D'https://groups.google.com/=
a/isocpp.org/d/msgid/std-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%40i=
socpp.org?utm_medium\x3demail\x26utm_source\x3dfooter';return true;" on=
click=3D"this.href=3D'https://groups.google.com/a/isocpp.org/d/msgid/st=
d-proposals/76362bfa-a887-4429-b371-3fca7c43c6e8%40isocpp.org?utm_medium\x3=
demail\x26utm_source\x3dfooter';return true;">https://groups.google.com=
/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/76362bfa-a887-4429-<wbr>b371-=
3fca7c43c6e8%40isocpp.org</a><wbr>.<br>
</blockquote></div>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/50fd77b0-d626-409b-b4de-6f05f3489e73%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/50fd77b0-d626-409b-b4de-6f05f3489e73=
%40isocpp.org</a>.<br />
------=_Part_896_339646981.1535364954933--
------=_Part_895_451579772.1535364954932--
.