ΒΆtemplate <class To, class From>
inline constexpr To cast(const From& from)

Description

An infallible conversion (cast) that may lose the original value in the process. See the [namespace level documentation]($sus::option) for more about converting types. If the input can not be represented in the output, some other value will be produced, which may lead to application bugs and memory unsafety if used incorrectly. This behaves like `static_cast <To >()` but does not cause Undefined Behaviour for any input and output type. without Undefined Behaviour. The [`cast`]($sus::construct::cast) operation is supported for types `To` and `From` that satisfy [`Cast <To , From>`]( $sus::construct::Cast). It is best practice to place a `// SAFETY:` comment on use of [`sus::cast`]( $sus::construct::cast) in order to explain why the code intends to change the value during the cast. # Examples This converts `-1` as an `i64` into a `u32`, which both changes its meaning, becoming a large positive number, and truncates the high 32 bits losing the original bits. ```cpp // SAFETY: We're intending to convert negative numbers into large positive // values for this example. sus_check(u32::MAX == sus::cast <u32 >(-1_i64)); ```

Declared at: sus/construct/cast.h:161

Templates

To
From

Parameters

const From& from