Document number: | P0116R0 |
Date: | 2015-09-25 |
Project: | Programming Language C++, Library Evolution Working Group |
Reply-to: | Robert Kawulak <Robert Kawulak at gmail dot com> |
bool
conversion operator to the following Standard Library types:
std::bitset
std::chrono::duration
std::complex
Types providing a boolean conversion operator offer a convenient way to check whether an object is in some kind of type-specific invalid or “empty” state. With the addition of explicit conversion operators and contextual conversions to C++11, such operators also have much less chance to cause confusion. Some built-in and Standard Library types provide boolean conversion, but there are still some types in the Standard Library that lack this feature despite its potential usefulness.
For example, it's been a common situation for the author to need to check a std::chrono::duration
value for being non-zero. Currently, this can be achieved like this:
std::chrono::seconds delay = get_delay();
if ( delay != std::chrono::seconds::zero() )
{
// there is some delay, handle it…
}
Or, alternatively, in a less verbose way:
std::chrono::seconds delay = get_delay();
if ( delay.count() )
{
// there is some delay, handle it…
}
The proposed addition would allow for expressing the same in a shorter and more readable way, for example:
std::chrono::seconds delay = get_delay();
if ( delay )
{
// there is some delay, handle it…
}
Or even:
if ( std::chrono::seconds delay = get_delay() )
{
// there is some delay, handle it…
}
bool
conversion operator in Standard Library types.
The author searched the Standard Library for types that have a notion of invalid or “empty” state but lack a bool
conversion operator. The types found are grouped into two categories: proposed and possible.
Note that these lists lack string, container and range types since there already was a proposal to add a bool
conversion operator to them and it has been rejected.
These types could have a bool
conversion operator with natural semantics and their usability would clearly benefit. The author proposes to add the operator to them. The types and the corresponding operator's semantics are presented in the table below.
type | operator bool () semantics |
---|---|
std::bitset | count() != 0 |
std::chrono::duration | *this != zero() |
std::complex | real() != T() || imag() != T() |
These types have some kind of invalid or “empty” state and technically could have a bool
conversion operator, but the author is not certain that the semantics are not potentially confusing or that there is obvious usability gain. Therefore the types are currently not included in the proposed types list, but the author is open to include those that the Committee would find worthwhile.
type | operator bool () semantics |
---|---|
std::weak_ptr | !expired() |
std::sub_match | matched |
std::match_results | ready() |
std::thread | get_id() != id() |
std::thread::id | *this != id() |
std::future | valid() |
std::shared_future | valid() |
std::packaged_task | valid() |
…
bool any() const noexcept;
bool none() const noexcept;
explicit operator bool() const noexcept;
…
…bool any() const noexcept;
45 Returns:count() != 0
bool none() const noexcept;
46 Returns:count() == 0
explicit operator bool() const noexcept;
47 Returns:count() != 0
…
…
// 20.12.5.2, observers:
constexpr rep count() const;
constexpr explicit operator bool() const;
…
20.12.5.2duration
observers [time.duration.observers]constexpr rep count() const;
1 Returns:rep_
.constexpr explicit operator bool() const;
2 Returns:*this != zero()
.
…
constexpr T imag() const;
void imag(T);
constexpr explicit operator bool() const;
…
…constexpr float imag() const; void imag(float); constexpr explicit operator bool() const;
…constexpr double imag() const; void imag(double); constexpr explicit operator bool() const;
…constexpr long double imag() const; void imag(long double); constexpr explicit operator bool() const;
…
…constexpr T imag() const;
Returns: The value of the imaginary component.void imag(T val);
Effects: Assignsval
to the imaginary component.constexpr explicit operator bool() const;
Returns:real() != T() || imag() != T()
.