Topic: Operator bool for ranges
Author: Olaf van der Spek <olafvdspek@gmail.com>
Date: Wed, 19 Dec 2012 02:18:25 -0800 (PST)
Raw View
------=_Part_34_19634445.1355912305265
Content-Type: text/plain; charset=ISO-8859-1
Follow up of
https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/pOjwxh73igY
Document number: Dnnnn=yy-nnnn
Date: 2012-12-19
Project: Programming Language C++, Library Working Group
Reply-to: Olaf van der Spek <olafvdspek@gmail.com>
// I. Table of Contents
II. Introduction
This proposal adds explicit operator bool() to strings, containers and
ranges.
III. Motivation and Scope
Bools can be true or false, integers can be zero or non-zero, (smart)
pointers can be null or non-null and strings, containers and ranges can be
empty or non-empty.
These states are checked frequently, but only pointers (and bools, ints)
can be checked directly. Strings, containers and ranges can't. Boost
iterator_range can, though.
This proposal makes possible such direct checking by adding explicit
operator bool() to all standard C++ strings, containers and ranges.
Direct checking is less verbose than using !empty() and allows code like
the following where a definition is used inside a condition.
std::string f() { return "Olaf"; }
int main()
{
if (std::string s = f())
use(s);
while (std::string s = f())
use(s);
}
IV. Impact on the Standard
This is a pure extension and can be implemented using C++11 compilers and
libraries.
// V. Design Decisions
VI. Technical Specifications
Add this function to all standard C++ strings, containers and ranges.
explicit operator bool() const { return !empty(); }
// VII. Acknowledgements
VIII. References
http://www.boost.org/doc/libs/1_52_0/libs/range/doc/html/range/reference/utilities/iterator_range.html
--
------=_Part_34_19634445.1355912305265
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div>Follow up of https://groups.google.com/a/isocpp.org/forum/#!topic/std-=
discussion/pOjwxh73igY<br></div><div><br></div><div>Document number: Dnnnn=
=3Dyy-nnnn</div><div>Date: 2012-12=
-19</div><div>Project: Programming Language C++=
, Library Working Group</div><div>Reply-to: Olaf=
van der Spek <olafvdspek@gmail.com></div><div><br></div><div><br></d=
iv><div>// I. Table of Contents</div><div><br></div><div><br></div><div>II.=
Introduction</div><div><br></div><div>This proposal adds explicit operator=
bool() to strings, containers and ranges.</div><div><br></div><div><br></d=
iv><div>III. Motivation and Scope</div><div><br></div><div>Bools can be tru=
e or false, integers can be zero or non-zero, (smart) pointers can be null =
or non-null and strings, containers and ranges can be empty or non-empty.</=
div><div>These states are checked frequently, but only pointers (and bools,=
ints) can be checked directly. Strings, containers and ranges can't. Boost=
iterator_range can, though.</div><div>This proposal makes possible such di=
rect checking by adding explicit operator bool() to all standard C++ string=
s, containers and ranges.</div><div><br></div><div>Direct checking is less =
verbose than using !empty() and allows code like the following where a defi=
nition is used inside a condition.</div><div><br></div><div>std::string f()=
{ return "Olaf"; }</div><div><br></div><div>int main()</div><div>{</div><d=
iv> if (std::string s =3D f())</div><div> use(s);</div><=
div><br></div><div> while (std::string s =3D f())</div><div> &n=
bsp; use(s);</div><div>}</div><div><br></div><div><br></div><div>IV. Impact=
on the Standard</div><div><br></div><div>This is a pure extension and can =
be implemented using C++11 compilers and libraries.</div><div><br></div><di=
v><br></div><div>// V. Design Decisions</div><div><br></div><div><br></div>=
<div>VI. Technical Specifications</div><div><br></div><div>Add this functio=
n to all standard C++ strings, containers and ranges.</div><div><br></div><=
div>explicit operator bool() const { return !empty(); }</div><div><br></div=
><div><br></div><div>// VII. Acknowledgements</div><div><br></div><div><br>=
</div><div>VIII. References</div><div><br></div><div>http://www.boost.org/d=
oc/libs/1_52_0/libs/range/doc/html/range/reference/utilities/iterator_range=
..html</div>
<p></p>
-- <br />
<br />
<br />
<br />
------=_Part_34_19634445.1355912305265--
.