LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
narrow.h
1/***************************************************************************
2 * LehrFEM++ - A simple C++ finite element libray for teaching
3 * Developed from 2018 at the Seminar of Applied Mathematics of ETH Zurich,
4 * lead developers Dr. R. Casagrande and Prof. R. Hiptmair
5 ***************************************************************************/
6
15#ifndef INCG_1944399668484015bcf9f347522a68c3
16#define INCG_1944399668484015bcf9f347522a68c3
17#include <type_traits>
18
19namespace lf::base {
20
30template <class TO, class FROM>
31auto narrow(FROM from) noexcept -> TO {
32 static_assert(std::is_arithmetic_v<TO>, "TO must be an arithmetic type");
33 const TO to = static_cast<TO>(std::forward<FROM>(from));
34 if constexpr (std::is_arithmetic_v<TO>) {
35 LF_ASSERT_MSG(static_cast<FROM>(to) == from &&
36 (std::is_signed_v<FROM> == std::is_signed_v<TO> ||
37 ((to < TO{}) == (from < FROM{}))),
38 "Narrowing error.");
39 } else {
40 LF_ASSERT_MSG(static_cast<FROM>(to) == from, "Narrowing error.");
41 }
42 return to;
43}
44
45} // namespace lf::base
46
47#endif // INCG_1944399668484015bcf9f347522a68c3
Contains basic functionality that is used by other parts of LehrFEM++.
Definition base.h:15
auto narrow(FROM from) noexcept -> TO
cast from to TO using static_cast. An assert will fail if this would change the value (e....
Definition narrow.h:31