From 8e96588829046656d8b51acf9ab21edb0dc37265 Mon Sep 17 00:00:00 2001 From: Erki Date: Sun, 6 Jun 2021 13:11:18 +0300 Subject: [PATCH] Add zeroInitialized to peripherals utility --- Peripherals/Inc/peripherals_utility.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Peripherals/Inc/peripherals_utility.hpp b/Peripherals/Inc/peripherals_utility.hpp index b708d34..077b683 100644 --- a/Peripherals/Inc/peripherals_utility.hpp +++ b/Peripherals/Inc/peripherals_utility.hpp @@ -9,6 +9,8 @@ #define PERIPHERALS_UTILITY_HPP_ #include +#include +#include namespace Peripherals { @@ -54,6 +56,17 @@ T ByteToTypeLE(const std::uint8_t a[N]) return t; } +template +constexpr T zeroInitialized() +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + + T t; + std::memset(&t, 0, sizeof(T)); + + return t; +} + }// namespace Peripherals #endif /* PERIPHERALS_UTILITY_HPP_ */