Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Library Documentation Index

Safe Numerics

PrevUpHomeNext

safe_signed_literal<Value, PP , EP> and safe_unsigned_literal<Value, PP, EP>

Description
Model of
Associated Types
Template Parameters
Inherited Valid Expressions
Valid Expression
Example of use
make_safe_literal(n, PP, EP)
Header

Description

A safe type which holds a literal value. This is required to be able to initialize other safe types in such a way that an exception code is not generated. It is also useful when creating constexpr versions of safe types. It contains one immutable value known at compile time and hence can be used in any constexpr expression.

Model of

Integer

SafeNumeric

This type inherits all the notation, associated types and template parameters and valid expressions of SafeNumeric types. The following specify additional features of this type.

Associated Types

PP A type which specifies the result type of an expression using safe types.
EP A type containing members which are called when a correct result cannot be returned

Template Parameters

Parameter Type Requirements Description
Value Integer value used to initialize the literal
PP PromotionPolicy<PP>

Default value is void

EP Exception Policy<EP>

Default value is void

Inherited Valid Expressions

safe literal types are immutable. Hence they only inherit those valid expressions which don't change the value. This excludes assignment, increment, and decrement and all unary operators except unary -, + and ~. Other than that, they can be used anywhere a SafeNumeric type can be used. Note that the default promotion and exception policies are void. This is usually convenient since when a safe literal is used in a binary operation, this will inherit the policies of the other type. On the other hand, this can be inconvenient when operands of a binary expression are both safe literals. This will fail to compile since there are no designated promotion and exception policies. The way to address this to assign specific policies as in this example.

template<typename T>
using compile_time_value = safe_signed_literal<T>;

constexpr compile_time_value<1000> x;
constexpr compile_time_value<0> y;

// should compile and execute without problem
    
std::cout << x << '\n';

// all the following statements should fail to compile because there are 
// no promotion and exception policies specified.
constexpr safe<int> z = x / y;

Valid Expression

Expression Result Type Description
t op s unspecified S

invoke C++ operator op and return another SafeNumeric type.

s1 op s2 unspecified S

invoke C++ operator op and return another SafeNumeric type.

prefix_op S unspecified S

invoke C++ operator prefix_op and return another SafeNumeric type.

S postfix_op unspecified S

invoke C++ operator postfix_op and return another SafeNumeric type.

s assign_op t S1

convert t to type S and assign it to s. If the value t cannot be represented as an instance of type S, it is an error.

S(t) unspecified S

construct an instance of S from a value of type T. If the value t cannot be represented as an instance of type S, it is an error.

S S

construct an uninitialized instance of S.

is_safe<S> std::true_type or std::false_type

type trait to query whether any type S fulfills the requirements for a SafeNumeric type.

static_cast<T>(s) T

convert the value of s to type T. If the value of s cannot be correctly represented as a type T, it is an error.

Example of use

#include <boost/numeric/safe_numerics/safe_literal.hpp>

constexpr boost::numeric::safe_signed_literal<42> x;

make_safe_literal(n, PP, EP)

This is a macro which returns an instance of a safe literal type. This instance will hold the value n. The type of the value returned will be the smallest safe type which can hold the value n.

Header

#include <boost/numeric/safe_numerics/safe_literal.hpp>


PrevUpHomeNext