cppx-core
Public Member Functions | List of all members
cppx::Truth Class Reference

A drop-in replacement for bool without implicit conversion from/to types other than bool. More...

#include <Truth.hpp>

Public Member Functions

template<class Result , class = Enable_if_<is_bool_<Result>>>
constexpr operator Result () const noexcept
 Implicit conversion to bool (only). More...
 
template<class Arg , class = Enable_if_<is_bool_<Arg>>>
constexpr Truth (const Arg value) noexcept
 Construction from bool (only). More...
 

Detailed Description

A drop-in replacement for bool without implicit conversion from/to types other than bool.

Implicit conversions to and from bool type are a common cause of undesired overload resolution. Truth only converts implicitly to and from bool. It's mostly a drop-in replacement for bool because you can use Truth values directly as conditions in if and loop statements, and in boolean operator expressions.

“Mostly”: as of C++17 Truth, like any other user-defined class type, can't be used as template value parameter type, but this may change with C++20.

Unplanned positive effect: indexing of a std::vector<Truth> produces a reference to a Truth item, which you can take the address of, rather than the proxy object that you usually get with a std::vector<bool>. The proxy objects of the latter allows it to store just 1 bit per item, at a cost that includes marginally reduced efficiency and high inconvenience. That cost is avoided with Truth as item type.

Definition at line 34 of file Truth.hpp.

Constructor & Destructor Documentation

◆ Truth()

template<class Arg , class = Enable_if_<is_bool_<Arg>>>
constexpr cppx::Truth::Truth ( const Arg  value)
inlinenoexcept

Construction from bool (only).

Construction SFINAE-restricted to bool argument.

Definition at line 59 of file Truth.hpp.

Member Function Documentation

◆ operator Result()

template<class Result , class = Enable_if_<is_bool_<Result>>>
constexpr cppx::Truth::operator Result ( ) const
inlinenoexcept

Implicit conversion to bool (only).

Implicit conversion to bool because that's needed for template args. Restricted via SFINAE because overload resolution, want predictability.

Definition at line 50 of file Truth.hpp.


The documentation for this class was generated from the following file: