1 | //-----------------------------------------------------------------------------
|
---|
2 | // boost variant/detail/forced_return.hpp header file
|
---|
3 | // See http://www.boost.org for updates, documentation, and revision history.
|
---|
4 | //-----------------------------------------------------------------------------
|
---|
5 | //
|
---|
6 | // Copyright (c) 2003
|
---|
7 | // Eric Friedman
|
---|
8 | //
|
---|
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)
|
---|
12 |
|
---|
13 | #ifndef BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP
|
---|
14 | #define BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP
|
---|
15 |
|
---|
16 | #include "boost/config.hpp"
|
---|
17 | #include "boost/variant/detail/generic_result_type.hpp"
|
---|
18 | #include "boost/assert.hpp"
|
---|
19 |
|
---|
20 | #if !defined(BOOST_MSVC) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
---|
21 | # include "boost/type_traits/remove_reference.hpp"
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | namespace boost {
|
---|
25 | namespace detail { namespace variant {
|
---|
26 |
|
---|
27 | ///////////////////////////////////////////////////////////////////////////////
|
---|
28 | // (detail) function template forced_return
|
---|
29 | //
|
---|
30 | // Logical error to permit invocation at runtime, but (artificially) satisfies
|
---|
31 | // compile-time requirement of returning a result value.
|
---|
32 | //
|
---|
33 |
|
---|
34 | #if !defined(BOOST_MSVC) \
|
---|
35 | && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
|
---|
36 | && !defined(BOOST_NO_VOID_RETURNS)
|
---|
37 |
|
---|
38 | // "standard" implementation:
|
---|
39 |
|
---|
40 | template <typename T>
|
---|
41 | inline T forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T) )
|
---|
42 | {
|
---|
43 | // logical error: should never be here! (see above)
|
---|
44 | BOOST_ASSERT(false);
|
---|
45 |
|
---|
46 | typedef typename boost::remove_reference<T>::type basic_type;
|
---|
47 | basic_type* dummy = 0;
|
---|
48 | return *static_cast< basic_type* >(dummy);
|
---|
49 | }
|
---|
50 |
|
---|
51 | template <>
|
---|
52 | inline void forced_return<void>( BOOST_EXPLICIT_TEMPLATE_TYPE_SPEC(void) )
|
---|
53 | {
|
---|
54 | // logical error: should never be here! (see above)
|
---|
55 | BOOST_ASSERT(false);
|
---|
56 | }
|
---|
57 |
|
---|
58 | #elif !defined(BOOST_MSVC)
|
---|
59 |
|
---|
60 | // workaround implementation
|
---|
61 | //
|
---|
62 | // TODO: Determine the most efficient way to handle this -- as below? by
|
---|
63 | // throwing? by recursive call to forced_return itself? etc.
|
---|
64 | //
|
---|
65 |
|
---|
66 | template <typename T>
|
---|
67 | inline
|
---|
68 | BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T)
|
---|
69 | forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T) )
|
---|
70 | {
|
---|
71 | // logical error: should never be here! (see above)
|
---|
72 | BOOST_ASSERT(false);
|
---|
73 |
|
---|
74 | BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T) (*dummy)() = 0;
|
---|
75 | return dummy();
|
---|
76 | }
|
---|
77 |
|
---|
78 | #else // defined(BOOST_MSVC)
|
---|
79 |
|
---|
80 | // msvc-specific implementation
|
---|
81 | //
|
---|
82 | // Leverages __declspec(noreturn) for optimized implementation.
|
---|
83 | //
|
---|
84 |
|
---|
85 | __declspec(noreturn)
|
---|
86 | inline void forced_return_no_return() {};
|
---|
87 |
|
---|
88 | template <typename T>
|
---|
89 | inline
|
---|
90 | BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(T)
|
---|
91 | forced_return( BOOST_EXPLICIT_TEMPLATE_TYPE(T) )
|
---|
92 | {
|
---|
93 | // logical error: should never be here! (see above)
|
---|
94 | BOOST_ASSERT(false);
|
---|
95 |
|
---|
96 | forced_return_no_return();
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endif // BOOST_MSVC optimization
|
---|
100 |
|
---|
101 | }} // namespace detail::variant
|
---|
102 | } // namespace boost
|
---|
103 |
|
---|
104 | #endif // BOOST_VARIANT_DETAIL_FORCED_RETURN_HPP
|
---|