1 //-----------------------------------------------------------------------------
2 // boost variant/detail/forced_return.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP
14 #define BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP
16 #include "boost/config.hpp"
17 #include "boost/variant/detail/generic_result_type.hpp"
18 #include "boost/assert.hpp"
20 #if !defined(BOOST_MSVC) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
21 # include "boost/type_traits/remove_reference.hpp"
25 namespace detail
{ namespace variant
{
27 ///////////////////////////////////////////////////////////////////////////////
28 // (detail) function template forced_return
30 // Logical error to permit invocation at runtime, but (artificially) satisfies
31 // compile-time requirement of returning a result value.
34 #if !defined(BOOST_MSVC) \
35 && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
36 && !defined(BOOST_NO_VOID_RETURNS)
38 // "standard" implementation:
41 inline T
forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T
) )
43 // logical error: should never be here! (see above)
46 typedef typename
boost::remove_reference
<T
>::type basic_type
;
47 basic_type
* dummy
= 0;
48 return *static_cast< basic_type
* >(dummy
);
52 inline void forced_return
<void>( BOOST_EXPLICIT_TEMPLATE_TYPE_SPEC(void) )
54 // logical error: should never be here! (see above)
58 #elif !defined(BOOST_MSVC)
60 // workaround implementation
62 // TODO: Determine the most efficient way to handle this -- as below? by
63 // throwing? by recursive call to forced_return itself? etc.
68 BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T
)
69 forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T
) )
71 // logical error: should never be here! (see above)
74 BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T
) (*dummy
)() = 0;
78 #else // defined(BOOST_MSVC)
80 // msvc-specific implementation
82 // Leverages __declspec(noreturn) for optimized implementation.
86 inline void forced_return_no_return() {};
90 BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T
)
91 forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T
) )
93 // logical error: should never be here! (see above)
96 forced_return_no_return();
99 #endif // BOOST_MSVC optimization
101 }} // namespace detail::variant
104 #endif // BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP