doruirimescu / cpp-scientific-units

Simple C++ 14 units of measurement library for type safety when dealing with physical units calculations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Header-only framework which allows using different units and quantities as types

C++ GitHub license

How to use

Example usage:

static_assert(static_cast<q_J>(1.0_q_J).value == 1.0);
constexpr auto grav_acc = 10.0_q_mps / 1.0_q_s;
constexpr auto height = 10.0_q_m;
constexpr auto mass = 10.0_q_g;
static_assert(static_cast<q_J>(1.0_q_J + 2.0_q_N * 2.5_q_m - grav_acc * height * mass).value == 5.0);

When dealing with prefixes, the first prefix appearing in the expression will be assigned to the resulting type:

auto mass =  10.0_q_g + 1.0_q_kg;
static_assert(std::is_same<decltype(mass), q_g>::value);
//******************************************************
auto mass =  1.0_q_kg + 10.0_q_g;
static_assert(std::is_same<decltype(mass), q_kg>::value);

It is possible to cast between different prefixes, as long as the unit is the same:

static_assert(static_cast<q_g>(q_kg{1.0}).value == 1000.0);

As you note, all types are starting with q_ and all user-defined type literals start with _q_. Conversion between equivalent types happen implicitly.

This code will generate a compile-time error.

q_g grams{10.0};
q_J energy = 2.0 * grams;

Selection_086

How it works

The framework consists of 3 building blocks:

  • metaprogramming
  • type_list
  • quantity

Metaprogramming

Metaprogramming is a set of helper templates which allow performing type manipulations on variadic structs.

Terminology: In this framework, a variadic/Variadic is any type declared as follows:

template<typename...T>
struct ExampleVariadic;

This, using the helper IsVariadic<ExampleVariadic<int, double>>::value will return true. IsVariadic<int>::value will return false.

The usage of the metaprogramming library can be easily understood from the unit tests.

Type lists

Type lists are built from putting all together the metaprogramming helpers into one class. Type lists are the building blocks of quantities.

For example, TypeList<kg, m> could represent a type of "kilogram times meter".

TypeList<s, s> could represent a type of "second square". Thus, a quantity will be represented by two type lists, one for the numerator and one for the denominator.

TypeList<> could represent a numerator or denominator of 1. For example, a quantity of meter could be represented by two type lists, TypeList<m> and TypeList<>, for the numerator and denominator respectively.

The metaprogramming helpers are used to define basic algebraic operations with TypeLists.

About

Simple C++ 14 units of measurement library for type safety when dealing with physical units calculations.


Languages

Language:C++ 95.1%Language:CMake 4.9%