1 // Copyright (c) 2019-2021 Antony Polukhin. 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #ifndef BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP 7 #define BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP 8 9 #include <boost/pfr/detail/config.hpp> 10 11 #include <type_traits> 12 13 namespace boost { namespace pfr { namespace detail { 14 15 // This function serves as a link-time assert. If linker requires it, then 16 // `unsafe_declval()` is used at runtime. 17 void report_if_you_see_link_error_with_this_function() noexcept; 18 19 // For returning non default constructible types. Do NOT use at runtime! 20 // 21 // GCCs std::declval may not be used in potentionally evaluated contexts, 22 // so we reinvent it. 23 template <class T> unsafe_declval()24constexpr T unsafe_declval() noexcept { 25 report_if_you_see_link_error_with_this_function(); 26 27 typename std::remove_reference<T>::type* ptr = 0; 28 ptr += 42; // suppresses 'null pointer dereference' warnings 29 return static_cast<T>(*ptr); 30 } 31 32 }}} // namespace boost::pfr::detail 33 34 35 #endif // BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP 36 37