unsafe_declval.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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. #ifndef BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP
  6. #define BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP
  7. #include <boost/pfr/detail/config.hpp>
  8. #include <type_traits>
  9. namespace boost { namespace pfr { namespace detail {
  10. // This function serves as a link-time assert. If linker requires it, then
  11. // `unsafe_declval()` is used at runtime.
  12. void report_if_you_see_link_error_with_this_function() noexcept;
  13. // For returning non default constructible types. Do NOT use at runtime!
  14. //
  15. // GCCs std::declval may not be used in potentionally evaluated contexts,
  16. // so we reinvent it.
  17. template <class T>
  18. constexpr T unsafe_declval() noexcept {
  19. report_if_you_see_link_error_with_this_function();
  20. typename std::remove_reference<T>::type* ptr = 0;
  21. ptr += 42; // suppresses 'null pointer dereference' warnings
  22. return static_cast<T>(*ptr);
  23. }
  24. }}} // namespace boost::pfr::detail
  25. #endif // BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP