Skip to content

Repository files navigation

value-preserving literals

GCC REUSE status fair-software.eu

The val.h header defines the user-defined literal _val and an equivalent val(x) function. The returned object can (and must) be converted to any arithmetic type. If the value changes on conversion, vir::bad_value_preserving_cast is thrown. All of this happens at compile time (consteval).

Example

#include <vir/val.h>

using vir::operator""_val;

auto displace(std::floating_point auto x)
{
  return x + 0x5EAF00D_val;
}

double a = displace(1.); // OK

float b = displace(1.f); // ill-formed

The displace<float> call fails because int(float(0x5EAF00D)) != 0x5EAF00D.

Installation

cmake -B build -DCMAKE_INSTALL_PREFIX=$HOME/.local
cmake --build build --target install

Alternatively, a Makefile is provided which allows installation via

make install prefix=$HOME/.local

About

adds literals to C++ that cannot change value when assigned to a type

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages