Table of Contents

Documentation

The newtype library provides types and functions to facilitate the creation of strong type aliases.

Example Usage

Note

All examples shown in this section can be found in the directory examples/src within the source root.

new-type-usage-basic below demonstrates the basic usage of new_type. In it, new_type is used to create thre new strong aliases Width, Height, and Area that all alias unsigned int.

However, using new_type in this fashion seem quite cumbersome. Starting from the bottom, unsigned int can normally be shifted on to any std::basic_ostream, like std::cout in this example. Since printing values, among other things, is a common scenario, newtype provides facilities to support automatic derivation of supporting functions.

new-type-usage-basic-show demonstrates how the function template deriving() can be used to enable automatic derivation of the stream output operator for Area. Similarly, it is possible to derive the stream input operators of Width and Height, as shown in new-type-usage-basic-read below.

API

This section of the documentation describes the public API of the new_type. It provides detailed descriptions of the types and functions designed to be used by applications. All declarations described in this section are found in the namespace nt, unless noted otherwise.

Header <newtype/new_type.hpp>

This header contains the definitions of the class template new_type as well as a set of associated namespace-level functions.

Class template new_type

template<typename BaseType, typename TagType, auto DerivationClause = deriving()>
class new_type

The class template new_type is designed to allow the creation of new types based on existing types. Similarly to the Haskell newtype, this class template creates a new type that is layout equivalent to the underlying type.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

New in version 1.0.0.

Member Type Aliases

using base_type = BaseType
using tag_type = TagType
using derivation_clause_type = decltype(DerivationClause)
using iterator = typename BaseType::iterator
Enablement

This type alias shall be defined iff. this new_type’s base_type has a member type iterator and the derivation clause contains Iterable.

New in version 1.1.0.

using const_iterator = typename BaseType::const_iterator
Enablement

This type alias shall be defined iff. this new_type’s base_type has a member type const_iterator and the derivation clause contains Iterable.

New in version 1.1.0.

using reverse_iterator = typename BaseType::reverse_iterator
Enablement

This type alias shall be defined iff. this new_type’s base_type has a member type reverse_iterator and the derivation clause contains Iterable.

New in version 1.1.0.

using const_reverse_iterator = typename BaseType::const_reverse_iterator
Enablement

This type alias shall be defined iff. this new_type’s base_type has a member type const_reverse_iterator and the derivation clause contains Iterable.

New in version 1.1.0.

Static Data Members

static derivation_clause_type constexpr derivation_clause = DerivationClause

Constructors

constexpr new_type()

Construct a new instance of this new_type by default constructing the contained object.

Throws

Any exception thrown by the default constructor of this new_type’s base_type. This constructor shall be noexcept iff. this new_type’s base_type is nothrow default-construtible.

Enablement

This constructor shall be defined as = default iff. this new_type’s base_type is default-construtible. Otherwise, this constructor shall be explicitely deleted.

constexpr new_type(new_type const &other)

Construct a new instance of this new_type by copy-constructing the contained object using the value contained by other.

Parameters

other – An existing instance of this new_type

Throws

Any exception thrown by the copy-constructor of this new_type’s base_type. This constructor shall be noexcept iff. this new_type’s base_type is nothrow copy-construtible.

Enablement

This constructor shall be defined as = default iff. this new_type’s base_type is copy-construtible. Otherwise, this constructor shall be explicitely deleted.

constexpr new_type(new_type &&other)

Construct a new instance of this new_type by move-constructing the contained object using the value contained by other.

Parameters

other – An existing instance of this new_type

Throws

Any exception thrown by the move-constructor of this new_type’s base_type. This constructor shall be noexcept iff. this new_type’s base_type is nothrow move-construtible.

Enablement

This constructor shall be defined as = default iff. this new_type’s base_type is move-construtible. Otherwise, this constructor shall be explicitely deleted.

constexpr new_type(BaseType const &value)

Construct a new instance of this new_type by copy-constructing the contained object using value.

Parameters

value – An existing instance of this new_type

Throws

Any exception thrown by the copy-constructor of this new_type’s base_type. This constructor shall be noexcept iff. this new_type’s base_type is nothrow copy-construtible.

Enablement

This constructor shall be defined as = default iff. this new_type’s base_type is copy-construtible. Otherwise, this constructor shall be explicitely deleted.

constexpr new_type(BaseType &&value)

Construct a new instance of this new_type by move-constructing the contained object using value.

Parameters

value – An existing instance of this new_type

Throws

Any exception thrown by the move-constructor of this new_type’s base_type. This constructor shall be noexcept iff. this new_type’s base_type is nothrow move-construtible.

Enablement

This constructor shall be defined as = default iff. this new_type’s base_type is move-construtible. Otherwise, this constructor shall be explicitely deleted.

Assignment Operators

constexpr new_type &operator=(new_type const &other)

Copy the value of an existing instance of this new_type and replace this instance’s value

Parameters

other – An existing instance of this new_type

Throws

Any exception thrown by the copy-assignment operator of this new_type’s base_type. This operator shall be noexcept iff. this new_type’s base_type is nothrow copy-assignable.

Enablement

This operator shall be defined as = default iff. this new_type’s base_type is copy-assignable. Otherwise, this operator shall be explicitely deleted.

constexpr new_type &operator=(new_type &&other)

Move the value of an existing instance of this new_type and replace this instance’s value

Parameters

other – An existing instance of this new_type

Throws

Any exception thrown by the move-assignment operator of this new_type’s base_type. This operator shall be noexcept iff. this new_type’s base_type is nothrow move-assignable.

Enablement

This operator shall be defined as = default iff. this new_type’s base_type is move-assignable. Otherwise, this operator shall be explicitely deleted.

Accessors

constexpr BaseType decay() const

Retrieve a copy of the object contained by this new_type object

Throws

Any exception thrown by the copy-constructor of this new_type’s base_type. This operator shall be noexcept iff. this new_type’s base_type is nothrow copy-constructible.

constexpr operator BaseType() const

Retrieve a copy of the object contained by this new_type object

Throws

Any exception thrown by the copy-constructor of this new_type’s base_type. This operator shall be noexcept iff. this new_type’s base_type is nothrow copy-constructible.

Explicit

This conversion operator shall be explicit unless this new_type’s derivation clause contains ImplicitConversion.

Member Access Through Pointer

constexpr BaseType operator->() noexcept

Perform “member access through pointer” via a pointer to object contained by this new_type

Enablement

This operator shall be available iff. this new_type’s derivation clause contains Indirection

constexpr BaseType const *operator->() const noexcept

Perform “member access through pointer” via a pointer to object contained by this new_type

Enablement

This operator shall be available iff. this new_type’s derivation clause contains Indirection

Iterators

constexpr iterator begin()

Get an iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function begin() that returns an instance of type iterator

New in version 1.1.0.

constexpr iterator begin() const

Get a constant iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function begin() const that returns an instance of type const_iterator

New in version 1.1.0.

constexpr iterator cbegin() const

Get a constant iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function cbegin() const that returns an instance of type const_iterator

New in version 1.1.0.

constexpr iterator rbegin()

Get a reverse iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function rbegin() that returns an instance of type reverse_iterator

New in version 1.1.0.

constexpr iterator rbegin() const

Get a constant reverse iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function rbegin() const that returns an instance of type const_reverse_iterator

New in version 1.1.0.

constexpr iterator crbegin() const

Get a constant reverse iterator to the beginning of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function crbegin() const that returns an instance of type const_reverse_iterator

New in version 1.1.0.

constexpr iterator end()

Get an iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function end() that returns an instance of type iterator

New in version 1.1.0.

constexpr iterator end() const

Get a constant iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function end() const that returns an instance of type const_iterator

New in version 1.1.0.

constexpr iterator cend() const

Get a constant iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function cend() const that returns an instance of type const_iterator

New in version 1.1.0.

constexpr iterator rend()

Get a reverse iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function rend() that returns an instance of type reverse_iterator

New in version 1.1.0.

constexpr iterator rend() const

Get a constant reverse iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function rend() const that returns an instance of type const_reverse_iterator

New in version 1.1.0.

constexpr iterator crend() const

Get a constant reverse iterator beyond the end of the object contained by this new_type

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. this new_type’s base type has a non-static member function crend() const that returns an instance of type const_reverse_iterator

New in version 1.1.0.

namespace-level functions and function templates

The functions and functions templates described in this section provide additional functionality for the class template new_type that is not part of the class itself.

Equality Comparison Operators
template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator==(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Check two instances of new_type<BaseType, TagType, DerivationClause> for equality.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of objects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow equals-comparable.

Enablement

This operator shall be available iff. new_type::base_type supports comparison using ==

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator==(new_type<BaseType, TagType, DerivationClause> const &lhs, BaseType const &rhs)

Check an instance of new_type<BaseType, TagType, DerivationClause> for equality with an instance of BaseType.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of object contained by lhs with an object of the base type.

Throws

Any exception thrown by the comparison of object contained by lhs with an object of the base type. This operator shall be noexcept iff. new_type::base_type is nothrow equals-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using == and

  2. the derivation clause contains EqBase

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator==(BaseType const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Check an instance of BaseType for equality with an instance of new_type<BaseType, TagType, DerivationClause>.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of an object of base type with the object contained by rhs.

Throws

Any exception thrown by the comparison of an object of base type with the object contained by rhs. This operator shall be noexcept iff. new_type::base_type is nothrow equals-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using == and

  2. the derivation clause contains EqBase

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator!=(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Check two instances of new_type<BaseType, TagType, DerivationClause> for in-equality.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of theobjects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow not-equals-comparable.

Enablement

This operator shall be available iff. new_type::base_type supports comparison using !=

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator!=(new_type<BaseType, TagType, DerivationClause> const &lhs, BaseType const &rhs)

Check an instance of new_type<BaseType, TagType, DerivationClause> for in-equality with an instance of BaseType.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the object contained by lhs with an object of the base type.

Throws

Any exception thrown by the comparison of the object contained by lhs with an object of the base type. This operator shall be noexcept iff. new_type::base_type is nothrow not-equals-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using != and

  2. the derivation clause contains EqBase

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator!=(BaseType const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Check an instance of BaseType for in-equality with an instance of new_type<BaseType, TagType, DerivationClause>.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of an object of base type with the object contained by rhs.

Throws

Any exception thrown by the comparison of an object of base type with the object contained by rhs. This operator shall be noexcept iff. new_type::base_type is nothrow not-equals-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using != and

  2. the derivation clause contains EqBase

New in version 1.0.0.

Relational Comparison Operators
template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator<(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Compare two instances of the same new_type using < (less-than).

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of the objects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow less-than-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using < and

  2. the derivation clause contains Relational

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator>(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Compare two instances of the same new_type using > (greater-than).

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of the objects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow greater-than-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using > and

  2. the derivation clause contains Relational

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator<=(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Compare two instances of the same new_type using <= (less-than-equal).

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of the objects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow less-than-equal-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using <= and

  2. the derivation clause contains Relational

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr bool operator>=(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Compare two instances of the same new_type using >= (greater-than-equal).

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the comparison

  • rhs – The right-hand side of the comparison

Returns

The value returned by the comparison of the contained objects.

Throws

Any exception thrown by the comparison operator of the objects contained by lhs and rhs. This operator shall be noexcept iff. new_type::base_type is nothrow greater-than-equal-comparable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports comparison using >= and

  2. the derivation clause contains Relational

New in version 1.0.0.

Stream I/O Operators
template<typename BaseType, typename TagType, auto DerivationClause, typename CharType, typename StreamTraits>
std::basic_ostream<CharType, StreamTraits> &operator<<(std::basic_ostream<CharType, StreamTraits> &out, new_type<BaseType, TagType, DerivationClause> const &value)

Write an instance of new_type<BaseType, TagType, DerivationClause> to a standard ostream.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

  • CharType – The stream character type

  • StreamTraits – The traits of the output stream

Parameters
  • out – The output stream

  • value – A new_type value to write to the output stream

Returns

A reference to the output stream

Throws

Any exception thrown by the stream-output operator of the object contained by value. This operator shall be noexcept iff. new_type::base_type is nothrow output-streamable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports being written to an output stream using << and

  2. the derivation clause contains Show

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause, typename CharType, typename StreamTraits>
std::basic_istream<CharType, StreamTraits> &operator>>(std::basic_istream<CharType, StreamTraits> &in, new_type<BaseType, TagType, DerivationClause> &value)

Read an instance of new_type<BaseType, TagType, DerivationClause> from a standard istream.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

  • CharType – The stream character type

  • StreamTraits – The traits of the input stream

Parameters
  • in – The input stream

  • value – A new_type value to be read from the output stream

Returns

A reference to the input stream

Throws

Any exception thrown by the stream-input operator of the object contained by value. This operator shall be noexcept iff. new_type::base_type is nothrow input-streamable.

Enablement

This operator shall be available iff.

  1. new_type::base_type supports being read from an input stream using >> and

  2. the derivation clause contains Read

New in version 1.0.0.

Arithmetic Operators
template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> operator+(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Add two instances of the same new_type.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the addition

  • rhs – The right-hand side of the addition

Returns

A new instance of new_type<BaseType, TagType, DerivationClause> containing the result of applying + to the objects contained by lhs and rhs.

Throws

Any exception thrown by the addition operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow addable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports addition using + and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> &operator+=(new_type<BaseType, TagType, DerivationClause> &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Add two instances of the same new_type by overwriting the first one.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the addition

  • rhs – The right-hand side of the addition

Returns

A reference to the first argument containing the value modified by applying += to the objects contained by lhs and rhs.

Throws

Any exception thrown by the addition-assignment operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow add-assignable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports addition using += and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> operator-(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Subtract two instances of the same new_type.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the subtraction

  • rhs – The right-hand side of the subtraction

Returns

A new instance of new_type<BaseType, TagType, DerivationClause> containing the result of applying - to the objects contained by lhs and rhs.

Throws

Any exception thrown by the subtraction operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow subtractable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports subtraction using - and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> &operator-=(new_type<BaseType, TagType, DerivationClause> &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Subtract two instances of the same new_type by overwriting the first one.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the subtraction

  • rhs – The right-hand side of the subtraction

Returns

A reference to the first argument containing the value modified by applying -= to the objects contained by lhs and rhs.

Throws

Any exception thrown by the subtraction-assignment operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow subtract-assignable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports subtraction using -= and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> operator*(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Multiply two instances of the same new_type.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the multiplication

  • rhs – The right-hand side of the multiplication

Returns

A new instance of new_type<BaseType, TagType, DerivationClause> containing the result of applying * to the objects contained by lhs and rhs.

Throws

Any exception thrown by the multiplication operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow multipliable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports multiplication using * and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> &operator*=(new_type<BaseType, TagType, DerivationClause> &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Multiply two instances of the same new_type by overwriting the first one.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the multiplication

  • rhs – The right-hand side of the multiplication

Returns

A reference to the first argument containing the value modified by applying *= to the objects contained by lhs and rhs.

Throws

Any exception thrown by the multiplication-assignment operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow multiply-assignable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports multiplication using *= and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> operator/(new_type<BaseType, TagType, DerivationClause> const &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Divide two instances of the same new_type.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the division

  • rhs – The right-hand side of the division

Returns

A new instance of new_type<BaseType, TagType, DerivationClause> containing the result of applying / to the objects contained by lhs and rhs.

Throws

Any exception thrown by the division operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow dividable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports division using / and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause> &operator/=(new_type<BaseType, TagType, DerivationClause> &lhs, new_type<BaseType, TagType, DerivationClause> const &rhs)

Divide two instances of the same new_type by overwriting the first one.

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters
  • lhs – The left-hand side of the division

  • rhs – The right-hand side of the division

Returns

A reference to the first argument containing the value modified by applying /= to the objects contained by lhs and rhs.

Throws

Any exception thrown by the division-assignment operator of the objects contained by lhs and rhs. This operator shall be noexcept iff.

  1. new_type::base_type is nothrow divide-assignable and

  2. new_type::base_type is nothrow copy-constructible

Enablement

This operator shall be available iff.

  1. new_type::base_type supports division using /= and

  2. the derivation clause contains Arithmetic

New in version 1.0.0.

Iterators
template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::iterator begin(new_type<BaseType, TagType, DerivationClause> &obj)

Get an iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function begin(BaseType &) that returns an instance of type new_type::iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_iterator begin(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function begin(BaseType const &) that returns an instance of type new_type::const_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_iterator cbegin(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function cbegin(BaseType const &) that returns an instance of type new_type::const_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::reverse_iterator rbegin(new_type<BaseType, TagType, DerivationClause> &obj)

Get a reverse iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function rbegin(BaseType &) that returns an instance of type new_type::reverse_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_reverse_iterator rbegin(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant reverse iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function rbegin(BaseType const &) that returns an instance of type new_type::const_reverse_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_reverse_iterator crbegin(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant reverse iterator to the beginning of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator to the begining of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function crbegin(BaseType const &) that returns an instance of type new_type::const_reverse_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::iterator end(new_type<BaseType, TagType, DerivationClause> &obj)

Get an iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function end(BaseType &) that returns an instance of type new_type::iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_iterator end(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function end(BaseType const &) that returns an instance of type new_type::const_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_iterator cend(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function cend(BaseType const &) that returns an instance of type new_type::const_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::reverse_iterator rend(new_type<BaseType, TagType, DerivationClause> &obj)

Get a reverse iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function rend(BaseType &) that returns an instance of type new_type::reverse_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_reverse_iterator rend(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant reverse iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function rend(BaseType const &) that returns an instance of type new_type::const_reverse_iterator

New in version 1.1.0.

template<typename BaseType, typename TagType, auto DerivationClause>
constexpr new_type<BaseType, TagType, DerivationClause>::const_reverse_iterator crend(new_type<BaseType, TagType, DerivationClause> const &obj)

Get a constant reverse iterator beyond the end of the object contained by an instance of new_type

Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Parameters

obj – The object to retrieve the iterator from

Returns

An iterator beyond the end of the object of contained by obj.

Throws

Any exception

Enablement

This function shall be available iff.

  1. this new_type’s derivation clause contains Iterable and

  2. for the new_type’s base type exists a namespace-level function crend(BaseType const &) that returns an instance of type new_type::const_reverse_iterator

New in version 1.1.0.

std::hash Support
template<typename BaseType, typename TagType, auto DerivationClause>
class std::hash<nt::new_type<BaseType, TagType, DerivationClause>>
Template Parameters
  • BaseType – The type of the contained object

  • TagType – A tag to uniquely identify an instance of nt::new_type

  • DerivationClause – A (possibly empty) list of derivation tags as generated by nt::deriving()

Hash an instance of new_type using the hash implementation of the base type.

constexpr std::size operator()(nt::new_type<BaseType, TagType, DerivationClause> const &value) const
Parameters

value – A nt::new_type value to be hashed

Returns

The result of applying std::hash to the object contained by value

Throws

Any exception thrown by the call operator of the specialization of :cpp:class`std::hash` for the type of the object contained by value.

Enablement

This operator shall be available iff.

  1. nt::new_type::base_type is hashable and

  2. the derivation clause contains Hash.

New in version 1.0.0.

Header <newtype/derivable.hpp>

This header defines the alias template derivable as well as the set of standard derivation tags.

Class template derivable

template<typename NameTag>
class derivable
Template Parameters

NameTag – A tag uniquely identifing a specific derivation tag

New in version 1.0.0.

Standard derivation tags

auto constexpr Arithmetic = derivable<class arithmetic_tag>{}

This tag enables the derivation of the following arithmetic operators:

New in version 1.0.0.

auto constexpr EqBase = derivable<class eq_base_tag>{}

This tag enables the derivation of following “equality comparison with base type” operators:

By virtue of its nature, deriving this feature compromises the strength of the given new_type.

New in version 1.0.0.

auto constexpr ImplicitConversion = derivable<class implicit_conversion_tag>{}

This tag enables the derivation of the implicit “conversion to base type” operator. By virtue of its nature, deriving this feature compromises the strength of the given new_type.

New in version 1.0.0.

auto constexpr Hash = derivable<class hash_tag>{}

This tag enables the derivation of a specialization of std::hash

New in version 1.0.0.

auto constexpr Indirection = derivable<class indirection_tag>{}

This tag enables the derivation of the “member access through pointer” operator operator->() (both in const and non-const variants).

New in version 1.0.0.

auto constexpr Iterable = derivable<class iterable_tag>{}

This tag enables the derivation of the following “standard iterator functions”:

New in version 1.1.0.

auto constexpr Read = derivable<class read_tag>{}

This tag enables the derivation of the “stream output” operator<<(std::basic_ostream &, new_type const &)

New in version 1.0.0.

auto constexpr Relational = derivable<class relational_tag>{}

This tag enables the derivation of the following relational operators:

New in version 1.0.0.

auto constexpr Show = derivable<class show_tag>{}

This tag enables the derivation of the “stream input” operator>>(std::basic_istream &, new_type &)

New in version 1.0.0.

Header <newtype/deriving.hpp>

This header contains the definition of the function template deriving().

Function template deriving()

template<typename ...DerivableTags>
constexpr derivation_clause<DerivableTags...> deriving(derivable<DerivableTags>... features) noexcept

This function can be used to create a new derivation_clause for use in the definitions of instances of new_type.

New in version 1.0.0.

See also

Standard derivation tags for a list of standard derivation tags

Header <newtype/derivation_clause.hpp>

This header contains the definition of the class template derivation_clause

Class template derivation_clause

template<typename ...DerivableTags>
class derivation_clause

Derivation clauses are used by new_type to allow users to specify a set of automatically derived support functions.

Template Parameters

DerivableTags – A (potentially empty) list of tag types identifying the contained derivations

New in version 1.0.0.

Constructors

constexpr derivation_clause(derivable<DerivableTags>...) noexcept

Construct a new derivations clause containing the given derivations

Evaluation Functions

template<typename DerivableTag>
constexpr bool operator()(derivable<DerivableTag>) const noexcept

Check if this derivation clause contains the given derivation

Template Parameters

DerivableTag – A tag uniquely identifying a derivation

template<typename DerivableTag, typename ...RemainingDerivableTags>
constexpr bool operator()(derivable<DerivableTag>, derivable<RemainingDerivableTags>...) const noexcept

Check if this derivation clause contains all of the given derivations

Template Parameters
  • DerivableTag – A tag uniquely identifying a derivation

  • RemainingDerivableTags – A list of tags uniquely identifying a list of derivations

Equality Comparison Operators

template<typename ...OtherDerivableTags>
constexpr bool operator==(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is identical to the one represented by other. Two derivation clauses are considered equal iff. both contain the same derivations irrespective of their order.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause

template<typename ...OtherDerivableTags>
constexpr bool operator!=(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is different from the one represented by other. Two derivation clauses are considered different iff. one contains at least one derivation not contained by the other.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause

Relational Comparison Operators

template<typename ...OtherDerivableTags>
constexpr bool operator<(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is a subset of the one represented by other. One derivation clause is considered to be a subset of another iff. the list of derivations of this instance forms a proper subset of the list of derivations of the other.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause

template<typename ...OtherDerivableTags>
constexpr bool operator>(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is a superset of the one represented by other. One derivation clause is considered to be a superset of another iff. the list of derivations of this instance forms a proper superset of the list of derivations of the other.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause

template<typename ...OtherDerivableTags>
constexpr bool operator<=(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is either identical to or a subset of the one represented by other. One derivation clause is considered to be identical to another iff. the list of derivations of this instance is identical to the list of derivations of the other. One derivation clause is considered to be a subset of another iff. the list of derivations of this instance forms a proper subset of the list of derivations of the other.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause

template<typename ...OtherDerivableTags>
constexpr bool operator>=(derivation_clause<OtherDerivableTags...> other) const noexcept

Check if this derivation clause is either identical to or a superset of the one represented by other. One derivation clause is considered to be identical to another iff. the list of derivations of this instance is identical to the list of derivations of the other. One derivation clause is considered to be a superset of another iff. the list of derivations of this instance forms a proper superset of the list of derivations of the other.

Template Parameters

OtherDerivableTags – A (potentialy empty) list of tags uniquely identifying a list of derivations

Parameters

other – An existing derivation clause