Boost.Hana  1.7.0
Your standard library for metaprogramming


The Group concept represents Monoids where all objects have an inverse w.r.t. the Monoid's binary operation.

A Group is an algebraic structure built on top of a Monoid which adds the ability to invert the action of the Monoid's binary operation on any element of the set. Specifically, a Group is a Monoid (S, +) such that every element s in S has an inverse (say ‘s’`) which is such that

s + s' == s' + s == identity of the Monoid

There are many examples of Groups, one of which would be the additive Monoid on integers, where the inverse of any integer n is the integer -n. The method names used here refer to exactly this model.

Minimal complete definitions

  1. minus
    When minus is specified, the negate method is defaulted by setting
    negate(x) = minus(zero<G>(), x)
  2. negate
    When negate is specified, the minus method is defaulted by setting
    minus(x, y) = plus(x, negate(y))


For all objects x of a Group G, the following laws must be satisfied:

plus(x, negate(x)) == zero<G>() // right inverse
plus(negate(x), x) == zero<G>() // left inverse

Refined concept


Concrete models


Free model for non-boolean arithmetic data types

A data type T is arithmetic if std::is_arithmetic<T>::value is true. For a non-boolean arithmetic data type T, a model of Group is automatically defined by setting

minus(x, y) = (x - y)
negate(x) = -x
The rationale for not providing a Group model for bool is the same as for not providing a Monoid model.

Structure-preserving functions

Let A and B be two Groups. A function f : A -> B is said to be a Group morphism if it preserves the group structure between A and B. Rigorously, for all objects x, y of data type A,

f(plus(x, y)) == plus(f(x), f(y))

Because of the Group structure, it is easy to prove that the following will then also be satisfied:

f(negate(x)) == negate(f(x))
f(zero<A>()) == zero<B>()

Functions with these properties interact nicely with Groups, which is why they are given such a special treatment.


constexpr auto boost::hana::minus
 Subtract two elements of a group. More...
constexpr auto boost::hana::negate
 Return the inverse of an element of a group. More...

Variable Documentation

◆ minus

constexpr auto boost::hana::minus

#include <boost/hana/fwd/minus.hpp>

Initial value:
= [](auto&& x, auto&& y) -> decltype(auto) {
return tag-dispatched;

Subtract two elements of a group.

Specifically, this performs the Monoid operation on the first argument and on the inverse of the second argument, thus being equivalent to:

minus(x, y) == plus(x, negate(y))

Cross-type version of the method

The minus method is "overloaded" to handle distinct data types with certain properties. Specifically, minus is defined for distinct data types A and B such that

  1. A and B share a common data type C, as determined by the common metafunction
  2. A, B and C are all Groups when taken individually
  3. to<C> : A -> B and to<C> : B -> C are Group-embeddings, as determined by the is_embedding metafunction.

The definition of minus for data types satisfying the above properties is obtained by setting

minus(x, y) = minus(to<C>(x), to<C>(y))


// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file or copy at
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTANT_CHECK(hana::minus(hana::int_c<3>, hana::int_c<5>) == hana::int_c<-2>);
static_assert(hana::minus(1, 2) == -1, "");

◆ negate

constexpr auto boost::hana::negate

#include <boost/hana/fwd/negate.hpp>

Initial value:
= [](auto&& x) -> decltype(auto) {
return tag-dispatched;

Return the inverse of an element of a group.


// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file or copy at
namespace hana = boost::hana;
int main() {
BOOST_HANA_CONSTANT_CHECK(hana::negate(hana::int_c<3>) == hana::int_c<-3>);
static_assert(hana::negate(2) == -2, "");
Defines boost::hana::equal.
Definition: assert.hpp:239
constexpr auto negate
Return the inverse of an element of a group.
Definition: negate.hpp:26
constexpr auto minus
Subtract two elements of a group.
Definition: minus.hpp:51
Defines boost::hana::integral_constant.
constexpr auto zero
Identity of plus.
Definition: zero.hpp:30
constexpr auto plus
Associative binary operation on a Monoid.
Definition: plus.hpp:47
Defines boost::hana::negate.
Defines boost::hana::minus.
Namespace containing everything in the library.
Definition: accessors.hpp:20
Defines macros to perform different kinds of assertions.