...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

Template version:

explicit quaternion(T const & requested_a = T(), T const & requested_b = T(), T const & requested_c = T(), T const & requested_d = T()); explicit quaternion(::std::complex<T> const & z0, ::std::complex<T> const & z1 = ::std::complex<T>()); template<typename X> explicit quaternion(quaternion<X> const & a_recopier);

Float specialization version:

explicit quaternion(float const & requested_a = 0.0f, float const & requested_b = 0.0f, float const & requested_c = 0.0f, float const & requested_d = 0.0f); explicit quaternion(::std::complex<float> const & z0,::std::complex<float> const & z1 = ::std::complex<float>()); explicit quaternion(quaternion<double> const & a_recopier); explicit quaternion(quaternion<long double> const & a_recopier);

Double specialization version:

explicit quaternion(double const & requested_a = 0.0, double const & requested_b = 0.0, double const & requested_c = 0.0, double const & requested_d = 0.0); explicit quaternion(::std::complex<double> const & z0, ::std::complex<double> const & z1 = ::std::complex<double>()); explicit quaternion(quaternion<float> const & a_recopier); explicit quaternion(quaternion<long double> const & a_recopier);

Long double specialization version:

explicit quaternion(long double const & requested_a = 0.0L, long double const & requested_b = 0.0L, long double const & requested_c = 0.0L, long double const & requested_d = 0.0L); explicit quaternion( ::std::complex<long double> const & z0, ::std::complex<long double> const & z1 = ::std::complex<long double>()); explicit quaternion(quaternion<float> const & a_recopier); explicit quaternion(quaternion<double> const & a_recopier);

A default constructor is provided for each form, which initializes each component to the default values for their type (i.e. zero for floating numbers). This constructor can also accept one to four base type arguments. A constructor is also provided to build quaternions from one or two complex numbers sharing the same base type. The unspecialized template also sports a templarized copy constructor, while the specialized forms have copy constructors from the other two specializations, which are explicit when a risk of precision loss exists. For the unspecialized form, the base type's constructors must not throw.

Destructors and untemplated copy constructors (from the same type) are provided by the compiler. Converting copy constructors make use of a templated helper function in a "detail" subnamespace.

T real() const; quaternion<T> unreal() const;

Like complex number, quaternions do have a meaningful notion of "real part", but unlike them there is no meaningful notion of "imaginary part". Instead there is an "unreal part" which itself is a quaternion, and usually nothing simpler (as opposed to the complex number case). These are returned by the first two functions.

T R_component_1() const; T R_component_2() const; T R_component_3() const; T R_component_4() const;

A quaternion having four real components, these are returned by these four functions. Hence real and R_component_1 return the same value.

::std::complex<T> C_component_1() const; ::std::complex<T> C_component_2() const;

A quaternion likewise has two complex components, and as we have seen above,
for any quaternion * q = α + βi + γj + δk* we also have

`q = (α + βi) + (γ + δi)j `

`q.C_component_1()`

is the same as `q.real()`

.
quaternion<T>& operator = (quaternion<T> const & a_affecter); template<typename X> quaternion<T>& operator = (quaternion<X> const& a_affecter); quaternion<T>& operator = (T const& a_affecter); quaternion<T>& operator = (::std::complex<T> const& a_affecter);

These perform the expected assignment, with type modification if necessary (for instance, assigning from a base type will set the real part to that value, and all other components to zero). For the unspecialized form, the base type's assignment operators must not throw.

quaternion<T>& operator += (T const & rhs) quaternion<T>& operator += (::std::complex<T> const & rhs); template<typename X> quaternion<T>& operator += (quaternion<X> const & rhs);

These perform the mathematical operation `(*this)+rhs`

and store the result in `*this`

.
The unspecialized form has exception guards, which the specialized forms
do not, so as to insure exception safety. For the unspecialized form, the
base type's assignment operators must not throw.

quaternion<T>& operator -= (T const & rhs) quaternion<T>& operator -= (::std::complex<T> const & rhs); template<typename X> quaternion<T>& operator -= (quaternion<X> const & rhs);

These perform the mathematical operation `(*this)-rhs`

and store the result in `*this`

.
The unspecialized form has exception guards, which the specialized forms
do not, so as to insure exception safety. For the unspecialized form, the
base type's assignment operators must not throw.

quaternion<T>& operator *= (T const & rhs) quaternion<T>& operator *= (::std::complex<T> const & rhs); template<typename X> quaternion<T>& operator *= (quaternion<X> const & rhs);

These perform the mathematical operation `(*this)*rhs`

**in this order** (order is important as multiplication
is not commutative for quaternions) and store the result in `*this`

. The
unspecialized form has exception guards, which the specialized forms do not,
so as to insure exception safety. For the unspecialized form, the base type's
assignment operators must not throw.

quaternion<T>& operator /= (T const & rhs) quaternion<T>& operator /= (::std::complex<T> const & rhs); template<typename X> quaternion<T>& operator /= (quaternion<X> const & rhs);

These perform the mathematical operation `(*this)*inverse_of(rhs)`

**in this order** (order is important as multiplication
is not commutative for quaternions) and store the result in `*this`

. The
unspecialized form has exception guards, which the specialized forms do not,
so as to insure exception safety. For the unspecialized form, the base type's
assignment operators must not throw.