From 4dc157951bd67d3d52e57545fa284583f0d0c252 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 1 Jan 2019 08:52:36 +1100 Subject: [PATCH] AP_Common: add template to check structure size This has the advantage of telling you what the sizes are In file included from ../../libraries/AC_AttitudeControl/AC_PosControl.h:3: ../../libraries/AP_Common/AP_Common.h:103:3: fatal error: static_assert failed "wrong size" static_assert(s == t, "wrong size"); ^ ~~~~~~ ../../libraries/AP_Common/AP_Common.h:147:52: note: in instantiation of template class 'assert_structure_size<12, 11>' requested here assert_structure_size _assert_location_size; --- libraries/AP_Common/AP_Common.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libraries/AP_Common/AP_Common.h b/libraries/AP_Common/AP_Common.h index 6ded872c81..7bf9f409e5 100644 --- a/libraries/AP_Common/AP_Common.h +++ b/libraries/AP_Common/AP_Common.h @@ -100,6 +100,20 @@ // @} +// assert_storage_size template: assert that the memory used to store an +// item is of a specific size. +// example invocation: +// assert_storage_size _assert_storage_size_Location; +// templates are used for this because the compiler's output will +// usually contain details of the template instantiation so you can +// see how the actual size differs from the expected size. +template struct _assert_storage_size { + static_assert(s_size == t, "wrong size"); +}; +template struct assert_storage_size { + _assert_storage_size _member; +}; + //////////////////////////////////////////////////////////////////////////////// /// @name Types ///