From 6a81ba6b23cace55abdb32b66ea2e4a4565d051b Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 11 Jan 2010 22:15:06 +0000 Subject: [PATCH] Added missing requires clause to the get() function of the type_safe_union. It will now cause a compile time error if called inappropriately. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403382 --- dlib/type_safe_union/type_safe_union_kernel.h | 21 ++++++++++++++++++- .../type_safe_union_kernel_abstract.h | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/dlib/type_safe_union/type_safe_union_kernel.h b/dlib/type_safe_union/type_safe_union_kernel.h index 7c9d44733..4be70a25e 100644 --- a/dlib/type_safe_union/type_safe_union_kernel.h +++ b/dlib/type_safe_union/type_safe_union_kernel.h @@ -301,7 +301,26 @@ namespace dlib } } - template T& get() { construct(); return *reinterpret_cast(mem.get()); } + template + T& get( + ) + { + // ERROR: You are trying to get a type of object that isn't currently + // in the type_safe_union. + COMPILE_TIME_ASSERT(( is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value || + is_same_type::value + )); + + construct(); return *reinterpret_cast(mem.get()); + } }; diff --git a/dlib/type_safe_union/type_safe_union_kernel_abstract.h b/dlib/type_safe_union/type_safe_union_kernel_abstract.h index 7be1f62e7..bcbb45e65 100644 --- a/dlib/type_safe_union/type_safe_union_kernel_abstract.h +++ b/dlib/type_safe_union/type_safe_union_kernel_abstract.h @@ -120,6 +120,8 @@ namespace dlib T& get( ); /*! + requires + - T must be one of the types given to this object's template arguments ensures - #is_empty() == false - #contains() == true