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>

Boolean conversion for Standard Library types

Introduction

The paper proposes addition of explicit bool conversion operator to the following Standard Library types: It also discusses possibility of adding the operator to other types.

Motivation

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…
}

Impact on the Standard

The proposal describes an extension to some Standard Library types that should generally be safe and have no impact on existing code. However, it might be a breaking change in rare cases, like in code explicitly checking for existence of bool conversion operator in Standard Library types.

Design Decisions

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.

Proposed

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()

Possible

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_resultsready()
std::thread get_id() != id()
std::thread::id *this != id()
std::future valid()
std::shared_futurevalid()
std::packaged_taskvalid()

Technical Specifications

The proposed wording is relative to the N4527 working draft. It only includes changes related to types listed in Proposed section.