Fixed possible UB

This commit is contained in:
Antony Polukhin
2014-03-06 10:32:23 +04:00
parent 501a0a992e
commit 3f51425f3c

View File

@ -42,17 +42,27 @@ namespace detail {
// 4) we need a compile-time control to make shure that user does not copy or
// default construct `struct-that-represents-type`
//
// Solution would be a forward declared structure.
struct ctti_data;
// Solution would be a standard-layout class with private constructors and assignment operators.
class ctti_data {
private:
ctti_data();
ctti_data(const ctti_data&);
ctti_data& operator=(const ctti_data&);
};
} // namespace detail
/// Helper method for getting detail::ctti_data of a tempalte patameter T.
template <class T>
inline const detail::ctti_data& ctti_construct() BOOST_NOEXCEPT {
// Standard C++11, 5.2.10 Reinterpret cast: Converting a prvalue of type “pointer to T1” to the
// type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no
// stricter than those of T1) and back to its original type yields the original pointer value.
// Standard C++11, 5.2.10 Reinterpret cast:
// An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue
// v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv
// T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment
// requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type
// “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment
// requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer
// value.
return *reinterpret_cast<const detail::ctti_data*>(boost::detail::ctti<T>::n());
}