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
This commit is contained in:
Davis King 2010-01-11 22:15:06 +00:00
parent b59e94cebf
commit 6a81ba6b23
2 changed files with 22 additions and 1 deletions

View File

@ -301,7 +301,26 @@ namespace dlib
}
}
template <typename T> T& get() { construct<T>(); return *reinterpret_cast<T*>(mem.get()); }
template <typename T>
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<T,T1>::value ||
is_same_type<T,T2>::value ||
is_same_type<T,T3>::value ||
is_same_type<T,T4>::value ||
is_same_type<T,T5>::value ||
is_same_type<T,T6>::value ||
is_same_type<T,T7>::value ||
is_same_type<T,T8>::value ||
is_same_type<T,T9>::value ||
is_same_type<T,T10>::value
));
construct<T>(); return *reinterpret_cast<T*>(mem.get());
}
};

View File

@ -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<T>() == true