CNL
2.0.2 (development)
Compositional Numeric Library
narrow_cast.h
1
2
// Copyright John McFarlane 2021.
3
// Distributed under the Boost Software License, Version 1.0.
4
// (See accompanying file ../../LICENSE_1_0.txt or copy at
5
// http://www.boost.org/LICENSE_1_0.txt)
6
7
#if !defined(CNL_IMPL_NARROW_CAST_H)
8
#define CNL_IMPL_NARROW_CAST_H
9
10
#include "../number.h"
11
#include "cnl_assert.h"
12
13
namespace
cnl::_impl {
14
template
<number To, number From>
15
[[nodiscard]]
inline
constexpr
auto
narrow_cast(From
const
& y) noexcept
16
{
17
auto
const
x =
static_cast<
To
>
(y);
18
CNL_ASSERT(
static_cast<
From
>
(x) == y);
19
return
x;
20
}
21
}
22
23
#endif // CNL_IMPL_NARROW_CAST_H
cnl
_impl
narrow_cast.h
Generated by
1.8.17