Topic: Add generic `operator<<` overload for `basic_ostream`


Author: Lingxi Li <lilingxi.cs@gmail.com>
Date: Fri, 22 Jan 2016 06:37:15 -0800 (PST)
Raw View
------=_Part_2829_545676642.1453473435302
Content-Type: multipart/alternative;
 boundary="----=_Part_2830_1807600125.1453473435303"

------=_Part_2830_1807600125.1453473435303
Content-Type: text/plain; charset=UTF-8

When some type T has overloaded `to_string()`, it is reasonable to also
overload
`operator<<()`, so you can do `cout << x` instead of `cout <<
to_string(x)`. It seems
rather verbose and redundant to overload `operator<<()` for each such type.
The
standard library should do something so that once `to_string()` is
overloaded, we
can get `cout << x` support for free. Specifically, I propose to add the
following two
overloads to the standard library.

template <class CharT, class Traits, class T>
typename enable_if<is_same<CharT, char>::value,
                   basic_ostream<CharT, Traits>&>::type
operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
  return os << to_string(x);
}

template <class CharT, class Traits, class T>
typename enable_if<is_same<CharT, wchar_t>::value,
                   basic_ostream<CharT, Traits>&>::type
operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
  return os << to_wstring(x);
}

ADL will pull in the namespace of `T`. The two overloads are least
specialized and
serve as a fallback when `os << x` will otherwise fail to compile.

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_2830_1807600125.1453473435303
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">When some type T has overloaded `to_string()`, it is reaso=
nable to also overload<br>`operator&lt;&lt;()`, so you can do `cout &lt;&lt=
; x` instead of `cout &lt;&lt; to_string(x)`. It seems<br>rather verbose an=
d redundant to overload `operator&lt;&lt;()` for each such type. The<br>sta=
ndard library should do something so that once `to_string()` is overloaded,=
 we<br>can get `cout &lt;&lt; x` support for free. Specifically, I propose =
to add the following two<br>overloads to the standard library.<br><br><div =
class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-w=
rap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pret=
typrint"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">template</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">class</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">CharT=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #60=
6;" class=3D"styled-by-prettify">Traits</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">class</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&g=
t;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">typename</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> enable_if</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">is_same</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D=
"color: #606;" class=3D"styled-by-prettify">CharT</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">char</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&gt;::</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">value</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0basic=
_ostream</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&l=
t;</span><span style=3D"color: #606;" class=3D"styled-by-prettify">CharT</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D=
"color: #606;" class=3D"styled-by-prettify">Traits</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&gt;&amp;&gt;::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">type<br></span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">operator</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">basic_ostream</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D=
"color: #606;" class=3D"styled-by-prettify">CharT</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">Traits</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&gt;&amp;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> os</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">const</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> x</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">return</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> os </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> to_string</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color: =
#008;" class=3D"styled-by-prettify">template</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">class</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prett=
ify">CharT</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">class</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #606;" class=3D"styled-by-prettify">Traits</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> T</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">type=
name</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> enabl=
e_if</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">is_same</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><spa=
n style=3D"color: #606;" class=3D"styled-by-prettify">CharT</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">wchar_t</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&gt;::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">value</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0basic_ostream</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">&lt;</span><span style=3D"color: #606;" class=3D"styled-by-pre=
ttify">CharT</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #606;" class=3D"styled-by-prettify">Traits</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&amp;&gt;::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">type<br></span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">operator</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;(</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">basic_ostream</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span =
style=3D"color: #606;" class=3D"styled-by-prettify">CharT</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" =
class=3D"styled-by-prettify">Traits</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;&amp;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> os</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">c=
onst</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> x</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> os </span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> to_wstring</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">x</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code></di=
v><br>ADL will pull in the namespace of `T`. The two overloads are least sp=
ecialized and<br>serve as a fallback when `os &lt;&lt; x` will otherwise fa=
il to compile.</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_2830_1807600125.1453473435303--
------=_Part_2829_545676642.1453473435302--

.


Author: Patrice Roy <patricer@gmail.com>
Date: Fri, 22 Jan 2016 21:54:17 -0500
Raw View
--089e01184e62a288440529f77320
Content-Type: text/plain; charset=UTF-8

Out of curosity: wouldn't this be a speed pessimization in a significant
amount of use-cases? I know I've never done such things in actual C++
production code (costs way too much), but I might have a skewed viewpoint...

Cheers!

2016-01-22 9:37 GMT-05:00 Lingxi Li <lilingxi.cs@gmail.com>:

> When some type T has overloaded `to_string()`, it is reasonable to also
> overload
> `operator<<()`, so you can do `cout << x` instead of `cout <<
> to_string(x)`. It seems
> rather verbose and redundant to overload `operator<<()` for each such
> type. The
> standard library should do something so that once `to_string()` is
> overloaded, we
> can get `cout << x` support for free. Specifically, I propose to add the
> following two
> overloads to the standard library.
>
> template <class CharT, class Traits, class T>
> typename enable_if<is_same<CharT, char>::value,
>                    basic_ostream<CharT, Traits>&>::type
> operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
>   return os << to_string(x);
> }
>
> template <class CharT, class Traits, class T>
> typename enable_if<is_same<CharT, wchar_t>::value,
>                    basic_ostream<CharT, Traits>&>::type
> operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
>   return os << to_wstring(x);
> }
>
> ADL will pull in the namespace of `T`. The two overloads are least
> specialized and
> serve as a fallback when `os << x` will otherwise fail to compile.
>
> --
>
> ---
> 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.
> Visit this group at
> https://groups.google.com/a/isocpp.org/group/std-proposals/.
>

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

--089e01184e62a288440529f77320
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Out of curosity: wouldn&#39;t this be a speed pessimi=
zation in a significant amount of use-cases? I know I&#39;ve never done suc=
h things in actual C++ production code (costs way too much), but I might ha=
ve a skewed viewpoint...<br><br></div>Cheers!<br></div><div class=3D"gmail_=
extra"><br><div class=3D"gmail_quote">2016-01-22 9:37 GMT-05:00 Lingxi Li <=
span dir=3D"ltr">&lt;<a href=3D"mailto:lilingxi.cs@gmail.com" target=3D"_bl=
ank">lilingxi.cs@gmail.com</a>&gt;</span>:<br><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr">When some type T has overloaded `to_string()`, it is rea=
sonable to also overload<br>`operator&lt;&lt;()`, so you can do `cout &lt;&=
lt; x` instead of `cout &lt;&lt; to_string(x)`. It seems<br>rather verbose =
and redundant to overload `operator&lt;&lt;()` for each such type. The<br>s=
tandard library should do something so that once `to_string()` is overloade=
d, we<br>can get `cout &lt;&lt; x` support for free. Specifically, I propos=
e to add the following two<br>overloads to the standard library.<br><br><di=
v style=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;backgroun=
d-color:rgb(250,250,250)"><code><div><span style=3D"color:#008">template</s=
pan><span style=3D"color:#000"> </span><span style=3D"color:#660">&lt;</spa=
n><span style=3D"color:#008">class</span><span style=3D"color:#000"> </span=
><span style=3D"color:#606">CharT</span><span style=3D"color:#660">,</span>=
<span style=3D"color:#000"> </span><span style=3D"color:#008">class</span><=
span style=3D"color:#000"> </span><span style=3D"color:#606">Traits</span><=
span style=3D"color:#660">,</span><span style=3D"color:#000"> </span><span =
style=3D"color:#008">class</span><span style=3D"color:#000"> T</span><span =
style=3D"color:#660">&gt;</span><span style=3D"color:#000"><br></span><span=
 style=3D"color:#008">typename</span><span style=3D"color:#000"> enable_if<=
/span><span style=3D"color:#660">&lt;</span><span style=3D"color:#000">is_s=
ame</span><span style=3D"color:#660">&lt;</span><span style=3D"color:#606">=
CharT</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> =
</span><span style=3D"color:#008">char</span><span style=3D"color:#660">&gt=
;::</span><span style=3D"color:#000">value</span><span style=3D"color:#660"=
>,</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0basic_ostream</span><span style=3D"color:=
#660">&lt;</span><span style=3D"color:#606">CharT</span><span style=3D"colo=
r:#660">,</span><span style=3D"color:#000"> </span><span style=3D"color:#60=
6">Traits</span><span style=3D"color:#660">&gt;&amp;&gt;::</span><span styl=
e=3D"color:#000">type<br></span><span style=3D"color:#008">operator</span><=
span style=3D"color:#660">&lt;&lt;(</span><span style=3D"color:#000">basic_=
ostream</span><span style=3D"color:#660">&lt;</span><span style=3D"color:#6=
06">CharT</span><span style=3D"color:#660">,</span><span style=3D"color:#00=
0"> </span><span style=3D"color:#606">Traits</span><span style=3D"color:#66=
0">&gt;&amp;</span><span style=3D"color:#000"> os</span><span style=3D"colo=
r:#660">,</span><span style=3D"color:#000"> </span><span style=3D"color:#00=
8">const</span><span style=3D"color:#000"> T</span><span style=3D"color:#66=
0">&amp;</span><span style=3D"color:#000"> x</span><span style=3D"color:#66=
0">)</span><span style=3D"color:#000"> </span><span style=3D"color:#660">{<=
/span><span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"color:#00=
8">return</span><span style=3D"color:#000"> os </span><span style=3D"color:=
#660">&lt;&lt;</span><span style=3D"color:#000"> to_string</span><span styl=
e=3D"color:#660">(</span><span style=3D"color:#000">x</span><span style=3D"=
color:#660">);</span><span style=3D"color:#000"><br></span><span style=3D"c=
olor:#660">}</span><span style=3D"color:#000"><br><br></span><span style=3D=
"color:#008">template</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">&lt;</span><span style=3D"color:#008">class</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#606">CharT</span><span styl=
e=3D"color:#660">,</span><span style=3D"color:#000"> </span><span style=3D"=
color:#008">class</span><span style=3D"color:#000"> </span><span style=3D"c=
olor:#606">Traits</span><span style=3D"color:#660">,</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#008">class</span><span style=3D"co=
lor:#000"> T</span><span style=3D"color:#660">&gt;</span><span style=3D"col=
or:#000"><br></span><span style=3D"color:#008">typename</span><span style=
=3D"color:#000"> enable_if</span><span style=3D"color:#660">&lt;</span><spa=
n style=3D"color:#000">is_same</span><span style=3D"color:#660">&lt;</span>=
<span style=3D"color:#606">CharT</span><span style=3D"color:#660">,</span><=
span style=3D"color:#000"> </span><span style=3D"color:#008">wchar_t</span>=
<span style=3D"color:#660">&gt;::</span><span style=3D"color:#000">value</s=
pan><span style=3D"color:#660">,</span><span style=3D"color:#000"><br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0basic_ost=
ream</span><span style=3D"color:#660">&lt;</span><span style=3D"color:#606"=
>CharT</span><span style=3D"color:#660">,</span><span style=3D"color:#000">=
 </span><span style=3D"color:#606">Traits</span><span style=3D"color:#660">=
&gt;&amp;&gt;::</span><span style=3D"color:#000">type<br></span><span style=
=3D"color:#008">operator</span><span style=3D"color:#660">&lt;&lt;(</span><=
span style=3D"color:#000">basic_ostream</span><span style=3D"color:#660">&l=
t;</span><span style=3D"color:#606">CharT</span><span style=3D"color:#660">=
,</span><span style=3D"color:#000"> </span><span style=3D"color:#606">Trait=
s</span><span style=3D"color:#660">&gt;&amp;</span><span style=3D"color:#00=
0"> os</span><span style=3D"color:#660">,</span><span style=3D"color:#000">=
 </span><span style=3D"color:#008">const</span><span style=3D"color:#000"> =
T</span><span style=3D"color:#660">&amp;</span><span style=3D"color:#000"> =
x</span><span style=3D"color:#660">)</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>=C2=A0=
 </span><span style=3D"color:#008">return</span><span style=3D"color:#000">=
 os </span><span style=3D"color:#660">&lt;&lt;</span><span style=3D"color:#=
000"> to_wstring</span><span style=3D"color:#660">(</span><span style=3D"co=
lor:#000">x</span><span style=3D"color:#660">);</span><span style=3D"color:=
#000"><br></span><span style=3D"color:#660">}</span><span style=3D"color:#0=
00"><br></span></div></code></div><br>ADL will pull in the namespace of `T`=
.. The two overloads are least specialized and<br>serve as a fallback when `=
os &lt;&lt; x` will otherwise fail to compile.</div><span class=3D"HOEnZb">=
<font color=3D"#888888">

<p></p>

-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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@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>
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/" target=3D"_blank">https://groups.google.com/a/isocpp.org/g=
roup/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

--089e01184e62a288440529f77320--

.


Author: Nevin Liber <nevin@cplusplusguy.com>
Date: Fri, 22 Jan 2016 21:01:06 -0600
Raw View
--001a11395ca861aea90529f78e2a
Content-Type: text/plain; charset=UTF-8

On 22 January 2016 at 20:54, Patrice Roy <patricer@gmail.com> wrote:

> Out of curosity: wouldn't this be a speed pessimization in a significant
> amount of use-cases? I know I've never done such things in actual C++
> production code (costs way too much), but I might have a skewed viewpoint...
>

It also introduces an exception-throwing failure mode, and turns a
SFINAE-detectable condition (no ostream inserter defined for a given type)
into a hard compile error.
--
 Nevin ":-)" Liber  <mailto:nevin@cplusplusguy.com <nevin@eviloverlord.com>>
+1-847-691-1404

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

--001a11395ca861aea90529f78e2a
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On 22 January 2016 at 20:54, Patrice Roy <span dir=3D"ltr"=
>&lt;<a href=3D"mailto:patricer@gmail.com" target=3D"_blank">patricer@gmail=
..com</a>&gt;</span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmai=
l_quote"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde=
r-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Out of curosi=
ty: wouldn&#39;t this be a speed pessimization in a significant amount of u=
se-cases? I know I&#39;ve never done such things in actual C++ production c=
ode (costs way too much), but I might have a skewed viewpoint...<br></div><=
/div></blockquote><div><br></div><div>It also introduces an exception-throw=
ing failure mode, and turns a SFINAE-detectable condition (no ostream inser=
ter defined for a given type) into a hard compile error.</div><div>-- <br><=
div><div dir=3D"ltr">=C2=A0Nevin &quot;:-)&quot; Liber=C2=A0 &lt;mailto:<a =
href=3D"mailto:nevin@eviloverlord.com" target=3D"_blank">nevin@cplusplusguy=
..com</a>&gt;=C2=A0 <a href=3D"tel:%2B1-847-691-1404" value=3D"+18476911404"=
 target=3D"_blank">+1-847-691-1404</a><br></div></div>
</div></div></div>
</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

--001a11395ca861aea90529f78e2a--

.


Author: Lingxi Li <lilingxi.cs@gmail.com>
Date: Fri, 22 Jan 2016 22:39:57 -0800 (PST)
Raw View
------=_Part_85_1891070335.1453531197526
Content-Type: multipart/alternative;
 boundary="----=_Part_86_1858139510.1453531197527"

------=_Part_86_1858139510.1453531197527
Content-Type: text/plain; charset=UTF-8


>
> and turns a SFINAE-detectable condition (no ostream inserter defined for a
> given type) into a hard compile error


It can be fixed as follows. Enable the overload only if `to_string(x)` is
well-formed.

template <class CharT, class Traits, class T,
          class = typename enable_if<is_same<CharT, char>::value>::type,
          class = typename enable_if<
                    true, decltype(to_string(declval<const T&>()))>::type>
std::basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
  return os << to_string(x);
}

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_86_1858139510.1453531197527
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"border-left-wid=
th: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; p=
adding-left: 1ex;">and turns a SFINAE-detectable condition (no ostream inse=
rter defined for a given type) into a hard compile error</blockquote><br>It=
 can be fixed as follows. Enable the overload only if `to_string(x)` is wel=
l-formed.<br><br><div class=3D"prettyprint" style=3D"border: 1px solid rgb(=
187, 187, 187); word-wrap: break-word; background-color: rgb(250, 250, 250)=
;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D=
"color: #008;" class=3D"styled-by-prettify">template</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">class</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">CharT</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">class</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #606;" class=3D"styled-by-prettify">Traits</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">class</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">class</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> enable_if</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
is_same</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt=
;</span><span style=3D"color: #606;" class=3D"styled-by-prettify">CharT</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">char</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">&gt;::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">value</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&gt;::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">type</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">class</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> enable_if</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">true</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">decltype</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">to_string</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">declval</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&am=
p;&gt;()))&gt;::</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">type</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>st=
d</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">basic_ostream</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><sp=
an style=3D"color: #606;" class=3D"styled-by-prettify">CharT</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606=
;" class=3D"styled-by-prettify">Traits</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&gt;&amp;</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">operator</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&lt;&lt;(</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">basic_ostream</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">&lt;</span><span style=3D"color: #606;" class=3D"style=
d-by-prettify">CharT</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">Traits</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&amp;</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> os</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> x</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">return</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> os </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> to_string</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">x</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span></div></code></div><div><br></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_86_1858139510.1453531197527--
------=_Part_85_1891070335.1453531197526--

.


Author: Zhihao Yuan <zy@miator.net>
Date: Sat, 23 Jan 2016 01:57:08 -0600
Raw View
On Sat, Jan 23, 2016 at 12:39 AM, Lingxi Li <lilingxi.cs@gmail.com> wrote:
>> and turns a SFINAE-detectable condition (no ostream inserter defined for a
>> given type) into a hard compile error
>
> It can be fixed as follows. Enable the overload only if `to_string(x)` is
> well-formed.

And you also need to follow the FormattedOutputFunction procedure:

  http://en.cppreference.com/w/cpp/concept/FormattedOutputFunction

But these are details.  The first question should be: How to choose the
customization point(s)?  Note that there is another paper trying to
provide to_string based on operator<<,

  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0117r0.html

, apparently this questions has to be answered first.  However, since
operator<< is already a customization point so you are expected to
see classes providing operator<< without to_string rather than the other
way, so at minimal you need to convince people about why to_string
is a better choice.

(Being said that, I'm not supporting defaulting anyone based on the other).

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: "T. C." <rs2740@gmail.com>
Date: Sat, 23 Jan 2016 00:05:49 -0800 (PST)
Raw View
------=_Part_735_1901414518.1453536349406
Content-Type: multipart/alternative;
 boundary="----=_Part_736_1910620447.1453536349406"

------=_Part_736_1910620447.1453536349406
Content-Type: text/plain; charset=UTF-8



On Saturday, January 23, 2016 at 1:39:57 AM UTC-5, Lingxi Li wrote:
>
> and turns a SFINAE-detectable condition (no ostream inserter defined for a
>> given type) into a hard compile error
>
>
> It can be fixed as follows. Enable the overload only if `to_string(x)` is
> well-formed.
>
> template <class CharT, class Traits, class T,
>           class = typename enable_if<is_same<CharT, char>::value>::type,
>           class = typename enable_if<
>                     true, decltype(to_string(declval<const T&>()))>::type>
> std::basic_ostream<CharT, Traits>&
> operator<<(basic_ostream<CharT, Traits>& os, const T& x) {
>   return os << to_string(x);
> }
>
>
This is a breaking change. Consider:

enum C : int { X = 16 } ;
std::cout << std::hex << X;

This now goes to operator<<(int), and prints '10'.

Under your proposal, it would print '16' (and is potentially-throwing).

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_736_1910620447.1453536349406
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<br><br>On Saturday, January 23, 2016 at 1:39:57 AM UTC-5, Lingxi Li wrote:=
<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><blockquote c=
lass=3D"gmail_quote" style=3D"border-left-width:1px;border-left-color:rgb(2=
04,204,204);border-left-style:solid;padding-left:1ex">and turns a SFINAE-de=
tectable condition (no ostream inserter defined for a given type) into a ha=
rd compile error</blockquote><br>It can be fixed as follows. Enable the ove=
rload only if `to_string(x)` is well-formed.<br><br><div style=3D"border:1p=
x solid rgb(187,187,187);word-wrap:break-word;background-color:rgb(250,250,=
250)"><code><div><span style=3D"color:#008">template</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#660">&lt;</span><span style=3D"col=
or:#008">class</span><span style=3D"color:#000"> </span><span style=3D"colo=
r:#606">CharT</span><span style=3D"color:#660">,</span><span style=3D"color=
:#000"> </span><span style=3D"color:#008">class</span><span style=3D"color:=
#000"> </span><span style=3D"color:#606">Traits</span><span style=3D"color:=
#660">,</span><span style=3D"color:#000"> </span><span style=3D"color:#008"=
>class</span><span style=3D"color:#000"> T</span><span style=3D"color:#660"=
>,</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
</span><span style=3D"color:#008">class</span><span style=3D"color:#000"> <=
/span><span style=3D"color:#660">=3D</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#008">typename</span><span style=3D"color:#000"> en=
able_if</span><span style=3D"color:#660">&lt;</span><span style=3D"color:#0=
00">is_same</span><span style=3D"color:#660">&lt;</span><span style=3D"colo=
r:#606">CharT</span><span style=3D"color:#660">,</span><span style=3D"color=
:#000"> </span><span style=3D"color:#008">char</span><span style=3D"color:#=
660">&gt;::</span><span style=3D"color:#000">value</span><span style=3D"col=
or:#660">&gt;::</span><span style=3D"color:#000">type</span><span style=3D"=
color:#660">,</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 </span><span style=3D"color:#008">class</span><span style=3D"col=
or:#000"> </span><span style=3D"color:#660">=3D</span><span style=3D"color:=
#000"> </span><span style=3D"color:#008">typename</span><span style=3D"colo=
r:#000"> enable_if</span><span style=3D"color:#660">&lt;</span><span style=
=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 </span><span style=3D"color:#008">true</span><span style=3D"=
color:#660">,</span><span style=3D"color:#000"> </span><span style=3D"color=
:#008">decltype</span><span style=3D"color:#660">(</span><span style=3D"col=
or:#000">to_string</span><span style=3D"color:#660">(</span><span style=3D"=
color:#000">declval</span><span style=3D"color:#660">&lt;</span><span style=
=3D"color:#008">con<wbr>st</span><span style=3D"color:#000"> T</span><span =
style=3D"color:#660">&amp;&gt;()))&gt;::</span><span style=3D"color:#000">t=
ype</span><span style=3D"color:#660">&gt;</span><span style=3D"color:#000">=
<br>std</span><span style=3D"color:#660">::</span><span style=3D"color:#000=
">basic_ostream</span><span style=3D"color:#660">&lt;</span><span style=3D"=
color:#606">CharT</span><span style=3D"color:#660">,</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#606">Traits</span><span style=3D"c=
olor:#660">&gt;&amp;</span><span style=3D"color:#000"><br></span><span styl=
e=3D"color:#008">operator</span><span style=3D"color:#660">&lt;&lt;(</span>=
<span style=3D"color:#000">basic_ostream</span><span style=3D"color:#660">&=
lt;</span><span style=3D"color:#606">CharT</span><span style=3D"color:#660"=
><wbr>,</span><span style=3D"color:#000"> </span><span style=3D"color:#606"=
>Traits</span><span style=3D"color:#660">&gt;&amp;</span><span style=3D"col=
or:#000"> os</span><span style=3D"color:#660">,</span><span style=3D"color:=
#000"> </span><span style=3D"color:#008">const</span><span style=3D"color:#=
000"> T</span><span style=3D"color:#660">&amp;</span><span style=3D"color:#=
000"> x</span><span style=3D"color:#660">)</span><span style=3D"color:#000"=
> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>=
=C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> os </span><span style=3D"color:#660">&lt;&lt;</span><span style=3D"c=
olor:#000"> to_string</span><span style=3D"color:#660">(</span><span style=
=3D"color:#000">x</span><span style=3D"color:#660">);</span><span style=3D"=
color:#000"><br></span><span style=3D"color:#660">}</span></div></code></di=
v><div><br></div></div></blockquote><div><br></div><div>This is a breaking =
change. Consider:=C2=A0</div><div><br></div><div class=3D"prettyprint" styl=
e=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgroun=
d-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"sub=
prettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">enum=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> C </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> X </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><font color=3D"#006666"><span style=3D"color: #066;" class=3D"styl=
ed-by-prettify">16</span></font><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br>std</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">cout </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> std</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">hex </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> X</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">;</span></div></code></div><div><br></div><div>This now goes to <fo=
nt face=3D"courier new, monospace">operator&lt;&lt;(int)</font>, and prints=
 &#39;10&#39;.</div><div><br></div><div>Under your proposal, it would print=
 &#39;16&#39; (and is potentially-throwing).</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_736_1910620447.1453536349406--
------=_Part_735_1901414518.1453536349406--

.


Author: Lingxi Li <lilingxi.cs@gmail.com>
Date: Sat, 23 Jan 2016 00:45:00 -0800 (PST)
Raw View
------=_Part_620_1095245556.1453538700429
Content-Type: multipart/alternative;
 boundary="----=_Part_621_53311126.1453538700429"

------=_Part_621_53311126.1453538700429
Content-Type: text/plain; charset=UTF-8


>
> Under your proposal, it would print '16' (and is potentially-throwing)


Why is this? I think the original overload will still be chosen, for it is
more specialized than the fallback version on the right-hand-side operand.

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_621_53311126.1453538700429
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"margin: 0px 0px=
 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); =
border-left-style: solid; padding-left: 1ex;">Under your proposal, it would=
 print &#39;16&#39; (and is potentially-throwing)</blockquote><br>Why is th=
is? I think the original overload will still be chosen, for it is more spec=
ialized than the fallback version on the right-hand-side operand.</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_621_53311126.1453538700429--
------=_Part_620_1095245556.1453538700429--

.


Author: Lingxi Li <lilingxi.cs@gmail.com>
Date: Sat, 23 Jan 2016 00:55:33 -0800 (PST)
Raw View
------=_Part_3809_1309521876.1453539333280
Content-Type: multipart/alternative;
 boundary="----=_Part_3810_543346262.1453539333281"

------=_Part_3810_543346262.1453539333281
Content-Type: text/plain; charset=UTF-8


>
> And you also need to follow the FormattedOutputFunction procedure:


 The fallback overload is least specialized. It only kicks in when the
program will
otherwise fail to compile.

so at minimal you need to convince people about why to_string is a better
> choice


I don't think so. People are free to choose whatever they want. I'm just
trying to
provide a default `operator <<` implementation if people have only
overloaded
`to_string()`. Besides, the default implementation can be overridden.

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_3810_543346262.1453539333281
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"margin: 0px 0px=
 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); =
border-left-style: solid; padding-left: 1ex;">And you also need to follow t=
he FormattedOutputFunction procedure:=C2=A0</blockquote><div><br>=C2=A0The =
fallback overload is least specialized. It only kicks in when the program w=
ill<br>otherwise fail to compile.<br><br><blockquote class=3D"gmail_quote" =
style=3D"margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-col=
or: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">so at=
 minimal you need to convince people about why to_string is a better choice=
</blockquote><br>I don&#39;t think so. People are free to choose whatever t=
hey want. I&#39;m just trying to<br>provide a default `operator &lt;&lt;` i=
mplementation if people have only overloaded<br>`to_string()`. Besides, the=
 default implementation can be overridden.</div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_3810_543346262.1453539333281--
------=_Part_3809_1309521876.1453539333280--

.


Author: "T. C." <rs2740@gmail.com>
Date: Sat, 23 Jan 2016 01:07:37 -0800 (PST)
Raw View
------=_Part_2714_819772042.1453540057224
Content-Type: multipart/alternative;
 boundary="----=_Part_2715_37748044.1453540057224"

------=_Part_2715_37748044.1453540057224
Content-Type: text/plain; charset=UTF-8



On Saturday, January 23, 2016 at 3:45:00 AM UTC-5, Lingxi Li wrote:
>
> Under your proposal, it would print '16' (and is potentially-throwing)
>
>
> Why is this? I think the original overload will still be chosen, for it is
> more specialized than the fallback version on the right-hand-side operand.
>

Your "fallback" is an exact match; the original overload requires a
promotion.

"more specialized" aka partial ordering (well, in this case it's actually
the template/non-template tiebreaker) only comes into play if the choices
are otherwise equally good. Templates tend to produce *very* good matches.

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_2715_37748044.1453540057224
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Saturday, January 23, 2016 at 3:45:00 AM UTC-5,=
 Lingxi Li wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:sol=
id;padding-left:1ex">Under your proposal, it would print &#39;16&#39; (and =
is potentially-throwing)</blockquote><br>Why is this? I think the original =
overload will still be chosen, for it is more specialized than the fallback=
 version on the right-hand-side operand.</div></blockquote><div><br></div><=
div>Your &quot;fallback&quot; is an exact match; the original overload requ=
ires a promotion.=C2=A0</div><div><br></div><div>&quot;more specialized&quo=
t; aka partial ordering (well, in this case it&#39;s actually the template/=
non-template tiebreaker) only comes into play if the choices are otherwise =
equally good. Templates tend to produce *very* good matches.</div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

------=_Part_2715_37748044.1453540057224--
------=_Part_2714_819772042.1453540057224--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 23 Jan 2016 14:38:16 +0100
Raw View
This is a multi-part message in MIME format.
--------------090207010702040201000606
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 22/01/2016 15:37, Lingxi Li a =C3=A9crit :
> When some type T has overloaded `to_string()`, it is reasonable to=20
> also overload
> `operator<<()`, so you can do `cout << x` instead of `cout <<=20
> to_string(x)`. It seems
> rather verbose and redundant to overload `operator<<()` for each such=20
> type. The
> standard library should do something so that once `to_string()` is=20
> overloaded, we
> can get `cout << x` support for free. Specifically, I propose to add=20
> the following two
> overloads to the standard library.
>
> |
> template<classCharT,classTraits,classT>
> typenameenable_if<is_same<CharT,char>::value,
>                    basic_ostream<CharT,Traits>&>::type
> operator<<(basic_ostream<CharT,Traits>&os,constT&x){
> returnos <<to_string(x);
> }
>
> template<classCharT,classTraits,classT>
> typenameenable_if<is_same<CharT,wchar_t>::value,
>                    basic_ostream<CharT,Traits>&>::type
> operator<<(basic_ostream<CharT,Traits>&os,constT&x){
> returnos <<to_wstring(x);
> }
> |
>
> ADL will pull in the namespace of `T`. The two overloads are least=20
> specialized and
> serve as a fallback when `os << x` will otherwise fail to compile.
>
I've found something like this useful while inserting ordinal enums in a=20
stream. These types use the C-string associated to the enumerator's=20
name, not its value. I have not found it useful for other types.

Instead of overloading to_string, I overload to_c_str(), which return=20
const char* and must be constexpr and noexcept. to_c_str is used when=20
the possible results are know at compile time, there is no allocation,=20
the result is a literal.

Note that to_c_str mustn't (cannot?) be defined for int as it is the=20
case for to_string and so the caveats signaled by Tony (T.C.) don't=20
appear in this case.

For example.

enum class E { a, b };

constexpr const char* to_c_str(E e) noexcept;

|template<classTraits>
basic_ostream<char,Traits>&>
operator<<(basic_ostream<CharT,Traits>&os,constC&x){
returnos <<to_c_str(x);
}


Adding a generic overload as you propose, but using for to_c_str=20
instead, could be useful. O||verloading |||to_c_str| for a type would=20
mean then that the author wants to insert it on a stream as a c-string.

I don't know if is worth adding it, as the needed code is minimal and it=20
would work only for ordinal enums.


|Vicente


--=20

---=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-propos=
als/.

--------------090207010702040201000606
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Type=
">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Le 22/01/2016 15:37, Lingxi Li a
      =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote
      cite=3D"mid:754ceee8-24dc-419c-8331-d8cfbb97a48c@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">When some type T has overloaded `to_string()`, it
        is reasonable to also overload<br>
        `operator&lt;&lt;()`, so you can do `cout &lt;&lt; x` instead of
        `cout &lt;&lt; to_string(x)`. It seems<br>
        rather verbose and redundant to overload `operator&lt;&lt;()`
        for each such type. The<br>
        standard library should do something so that once `to_string()`
        is overloaded, we<br>
        can get `cout &lt;&lt; x` support for free. Specifically, I
        propose to add the following two<br>
        overloads to the standard library.<br>
        <br>
        <div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187,
          187); word-wrap: break-word; background-color: rgb(250, 250,
          250);"><code class=3D"prettyprint">
            <div class=3D"subprettyprint"><span style=3D"color: #008;"
                class=3D"styled-by-prettify">template</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> T</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">typename</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                enable_if</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">&lt;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">is_same=
</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">char</s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;::<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">value</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0basic_ostream</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&am=
p;&gt;::</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">type<br=
>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">operator</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt=
;(</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">basic_o=
stream</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&am=
p;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> os</sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">const</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> T</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> x</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">{</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">return</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> os </s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt=
;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                to_string</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">(</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify">x</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">}</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify"><br>
                <br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">template</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">class</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> T</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">typename</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                enable_if</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">&lt;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">is_same=
</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">wchar_t=
</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;::<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">value</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0basic_ostream</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&am=
p;&gt;::</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">type<br=
>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">operator</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt=
;(</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">basic_o=
stream</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Traits<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&am=
p;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> os</sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">const</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> T</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> x</spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">{</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">return</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> os </s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt=
;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                to_wstring</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">(</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify">x</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">}</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify"><br>
              </span></div>
          </code></div>
        <br>
        ADL will pull in the namespace of `T`. The two overloads are
        least specialized and<br>
        serve as a fallback when `os &lt;&lt; x` will otherwise fail to
        compile.</div>
      <br>
    </blockquote>
    I've found something like this useful while inserting ordinal enums
    in a stream. These types use the C-string associated to the
    enumerator's name, not its value. I have not found it useful for
    other types.<br>
    <br>
    Instead of overloading to_string, I overload to_c_str(), which
    return const char* and must be constexpr and noexcept. to_c_str is
    used when the possible results are know at compile time, there is no
    allocation, the result is a literal.<br>
    <br>
    Note that to_c_str mustn't (cannot?) be defined for int as it is the
    case for to_string and so the caveats signaled by Tony (T.C.) don't
    appear in this case.<br>
    <br>
    For example.<br>
    <br>
    enum class E { a, b };<br>
    <br>
    constexpr const char* to_c_str(E e) noexcept;<br>
    <br>
    <code class=3D"prettyprint"><span style=3D"color: #008;"
        class=3D"styled-by-prettify">template</span><span style=3D"color:
        #000;" class=3D"styled-by-prettify"> </span><span style=3D"color:
        #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color=
:
        #000;" class=3D"styled-by-prettify"></span><span style=3D"color:
        #008;" class=3D"styled-by-prettify">class</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span
        style=3D"color: #606;" class=3D"styled-by-prettify">Traits</span><s=
pan
        style=3D"color: #000;" class=3D"styled-by-prettify"></span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><spa=
n
        style=3D"color: #000;" class=3D"styled-by-prettify"><br>
      </span><span style=3D"color: #000;" class=3D"styled-by-prettify">basi=
c_ostream</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><spa=
n
        style=3D"color: #606;" class=3D"styled-by-prettify">char</span><spa=
n
        style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span
        style=3D"color: #606;" class=3D"styled-by-prettify">Traits</span><s=
pan
        style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&amp;&gt;</=
span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"><br>
      </span><span style=3D"color: #008;" class=3D"styled-by-prettify">oper=
ator</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;(</span=
><span
        style=3D"color: #000;" class=3D"styled-by-prettify">basic_ostream</=
span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><spa=
n
        style=3D"color: #606;" class=3D"styled-by-prettify">CharT</span><sp=
an
        style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span
        style=3D"color: #606;" class=3D"styled-by-prettify">Traits</span><s=
pan
        style=3D"color: #660;" class=3D"styled-by-prettify">&gt;&amp;</span=
><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> os</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span
        style=3D"color: #008;" class=3D"styled-by-prettify">const</span><sp=
an
        style=3D"color: #000;" class=3D"styled-by-prettify"> C</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><sp=
an
        style=3D"color: #000;" class=3D"styled-by-prettify"> x</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"><br>
        =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">return</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"> os </span><spa=
n
        style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span>=
<span
        style=3D"color: #000;" class=3D"styled-by-prettify"> to_c_str</span=
><span
        style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span
        style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span
        style=3D"color: #000;" class=3D"styled-by-prettify"><br>
      </span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</s=
pan><span
        style=3D"color: #000;" class=3D"styled-by-prettify"><br>
        <br>
        <br>
        Adding a generic overload as you propose, but using for to_c_str
        instead, could be useful. O</span></code><code
      class=3D"prettyprint"><span style=3D"color: #000;"
        class=3D"styled-by-prettify">verloading </span></code><code
      class=3D"prettyprint"><span style=3D"color: #000;"
        class=3D"styled-by-prettify"><code class=3D"prettyprint"><span
            style=3D"color: #000;" class=3D"styled-by-prettify">to_c_str</s=
pan></code>
        for a type would mean then that the author wants to insert it on
        a stream as a c-string.<br>
        <br>
        I don't know if is worth adding it, as the needed code is
        minimal and it would work only for ordinal enums.<br>
        <br>
        <br>
      </span></code>Vicente<br>
    <br>
    <br>
  </body>
</html>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

--------------090207010702040201000606--

.


Author: Moritz Klammler <moritz.klammler@gmail.com>
Date: Sat, 23 Jan 2016 17:59:10 +0100
Raw View
Lingxi Li <lilingxi.cs@gmail.com> writes:

> When some type T has overloaded `to_string()`, it is reasonable to
> also overload `operator<<()`, so you can do `cout << x` instead of
> `cout << to_string(x)`. It seems rather verbose and redundant to
> overload `operator<<()` for each such type.  The standard library
> should do something so that once `to_string()` is overloaded, we can
> get `cout << x` support for free. Specifically, I propose to add the
> following two overloads to the standard library.

This question has been discussed on Stack Overflow before.  (I assume
you are the author of that post.)

    https://stackoverflow.com/questions/34940754/fallback-to-to-string-when-operator-fails

As I've mentioned in my answer there, `to_string` is just one of many
possible names.  It is true that the standard library defines such
functions for integral types (but fore those, `operator<<` is already
overloaded anyway and ADL doesn't work for them) but then again, other
types from the standard library like string streams rather have a `str`
member function.  `std::exception` has a `what` member function.  Other
people might use even other spellings.  If a type has both, an
ADL-findable `to_string` overload and a `str` member function, which one
do you want?  Who is going to decide what names can reasonably be
expected to mean "this converts the type into a string representation"?

I believe that such a generic adapter can be useful for some code bases
where you can enable it explicitly.  But the standard library should
stay away from adding a possibly confusing feature.  I think that the
"official" guideline should remain: If you want your type to be
streamable, overload `operator<<` for it.

Finally, I think that it could often be easier to go the other way round
and implement `to_string` via `operator<<` (aka lexical cast).

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: Lingxi Li <lilingxi.cs@gmail.com>
Date: Sun, 24 Jan 2016 03:44:52 -0800 (PST)
Raw View
------=_Part_4985_316098373.1453635892330
Content-Type: multipart/alternative;
 boundary="----=_Part_4986_939175039.1453635892330"

------=_Part_4986_939175039.1453635892330
Content-Type: text/plain; charset=UTF-8

I see. Your arguments are sound and reasonable. Such a facility doesn't seem
to fit into the standard library.

--

---
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.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_4986_939175039.1453635892330
Content-Type: text/html; charset=UTF-8

<div dir="ltr">I see. Your arguments are sound and reasonable. Such a facility doesn&#39;t seem<br>to fit into the standard library.</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />

------=_Part_4986_939175039.1453635892330--
------=_Part_4985_316098373.1453635892330--

.