boolean-lite

boolean lite: A strong boolean type for C++98 and later

BSL-1.0 License

Stars
5

boolean lite: A strong boolean type for C++98 and later

Contents

Example usage

#include "nonstd/boolean.hpp"
#include <iostream>

using nonstd::boolean_;

void eat_cookies( int count, boolean_ leave_crumbs )
{
    std::cout << "Eat " << count << " cookies and leave " << (leave_crumbs ? "" : "no ") << "crumbs\n";
}

void santa( int num_cookies )
{
    const boolean_ leave_crumbs = num_cookies > 4;

//  eat_cookies( leave_crumbs, num_cookies );  // Does not compile: wrong argument order
    eat_cookies( num_cookies, leave_crumbs );  // Ok
}

int main()
{
    santa( 3 );
    santa( 5 );
}

Compile and run

prompt >g++ -std=c++11 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
Eat 3 cookies and leave no crumbs
Eat 5 cookies and leave crumbs

In a nutshell

boolean lite is a single-file header-only library to provide a strong boolean type.

boolean lite is inspired on the article Fun with Concepts: Do You Even Lift, Bool? by Colby Pike (vector-of-bool), Oct 26, 2020.

Features and properties of boolean lite are ease of installation (single header), freedom of dependencies other than the standard library.

License

boolean lite is distributed under the Boost Software License.

Dependencies

boolean lite has no other dependencies than the C++ standard library.

Installation and use

boolean lite is a single-file header-only library. Put boolean.hpp in the include folder directly into the project source tree or somewhere reachable from your project.

Synopsis

Contents Documentation of nonstd::boolean Configuration

Documentation of nonstd::boolean

The behaviour of boolean differs under C++98 and C++11 and later. Specifically the C++11 version allows the following that is not supported by the C++98 version of boolean_.

  • copy initialization like boolean_ b = true;
  • comparison to bool, like boolean_ f( false ); boolean_ result = f == true;

Note: The name boolean_ is used for the type to prevent clashing with type boolean from the Windows SDK (rpcndr.h(193)).

Kind std Function or method
Construction C++11 template<typename T [bool]> constexpr boolean_(T v) noexcept;
  template<typename T [!bool]> explicit constexpr boolean_(T v) noexcept;
Conversion template<typename T [bool]> [[nodiscard]] constexpr operator T() const noexcept;
  template<typename T [!bool]> [[nodiscard]] explicit constexpr operator T() const noexcept;
Negation [[nodiscard]] friend explicit constexpr operator!() const noexcept;
Equality [[nodiscard]] friend explicit constexpr operator==(boolean_ a, boolean_ b) noexcept;
  [[nodiscard]] friend explicit constexpr operator!=(boolean_ a, boolean_ b) noexcept;
Ordering [[nodiscard]] friend explicit constexpr operator<(boolean_ a, boolean_ b) noexcept;
  [[nodiscard]] friend explicit constexpr operator>(boolean_ a, boolean_ b) noexcept;
  [[nodiscard]] friend explicit constexpr operator<=(boolean_ a, boolean_ b) noexcept;
  C++11 [[nodiscard]] friend explicit constexpr operator>=(boolean_ a, boolean_ b) noexcept;
     
Construction  C++98  explicit boolean_(bool v);Note: no copy initialization: boolean_ b = true;
Conversion    operator bool_type() const;Note: bool_type allows for safe conversions
Negation boolean_ operator!() const;
Equality friend boolean_ operator==(boolean_ a, boolean_ b);
  friend boolean_ operator!=(boolean_ a, boolean_ b);
Ordering friend boolean_ operator<(boolean_ a, boolean_ b);
  friend boolean_ operator>(boolean_ a, boolean_ b);
  friend boolean_ operator<=(boolean_ a, boolean_ b);
  C++98 friend boolean_ operator>=(boolean_ a, boolean_ b);

Configuration

Tweak header

If the compiler supports __has_include(), boolean lite supports the tweak header mechanism. Provide your tweak header as nonstd/boolean.tweak.hpp in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like #define boolean_CPLUSPLUS 201103L.

Standard selection macro

-Dboolean_CPLUSPLUS=199711L Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cplusplus macro correctly.

Notes and references

Appendix

A.1 Compile-time information

The version of boolean lite is available via tag [.version]. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler], [.stdc++], [.stdlanguage] and [.stdlibrary].

A.2 Span lite test specification

boolean: Allows to direct initialize from bool
boolean: Allows to copy initialize from bool (C++11)
boolean: Allows to direct initialize from type convertible to bool
boolean: Allows to compare equal
boolean: Allows to compare unequal
boolean: Allows to compare less than
boolean: Allows to compare less than or equal
boolean: Allows to compare greater than
boolean: Allows to compare greater than or equal
boolean: Allows to compare equal with bool (C++11)
boolean: Allows to compare unequal with bool (C++11)
boolean: Allows to compare less than with bool (C++11)
boolean: Allows to compare less than or equal with bool (C++11)
boolean: Allows to compare greater than with bool (C++11)
boolean: Allows to compare greater than or equal with bool (C++11)
boolean: Supports no discard (C++17, -DBOOLEAN_TEST_NODISCARD=1)
tweak header: Reads tweak header if supported [tweak]
Badges
Extracted from project README
Language License Build Status Build status Version download Conan Try it on wandbox Try it on godbolt online
Related Projects