From: Justin Viiret Date: Mon, 1 May 2017 04:21:51 +0000 (+1000) Subject: verify_types: add type static assertions X-Git-Tag: v4.5.0^2~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a04d1330e680c5d6c2be4a7a82facc955f19e06;p=thirdparty%2Fvectorscan.git verify_types: add type static assertions --- diff --git a/src/util/verify_types.h b/src/util/verify_types.h index 148b4377..5833d5ec 100644 --- a/src/util/verify_types.h +++ b/src/util/verify_types.h @@ -33,16 +33,25 @@ #include "util/compile_error.h" #include +#include namespace ue2 { template To_T verify_cast(From_T val) { + static_assert(std::is_integral::value, + "Output type must be integral."); + static_assert(std::is_integral::value || + std::is_enum::value || + std::is_convertible::value, + "Must be integral or enum type, or convertible to output."); + To_T conv_val = static_cast(val); if (static_cast(conv_val) != val) { assert(0); throw ResourceLimitError(); } + return conv_val; }