unsafe_declval.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2019-2025 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. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #if !defined(BOOST_PFR_INTERFACE_UNIT)
  10. #include <type_traits>
  11. #endif
  12. namespace boost { namespace pfr { namespace detail {
  13. // This function serves as a link-time assert. If linker requires it, then
  14. // `unsafe_declval()` is used at runtime.
  15. void report_if_you_see_link_error_with_this_function() noexcept;
  16. // For returning non default constructible types. Do NOT use at runtime!
  17. //
  18. // GCCs std::declval may not be used in potentionally evaluated contexts,
  19. // so we reinvent it.
  20. template <class T>
  21. constexpr T unsafe_declval() noexcept {
  22. report_if_you_see_link_error_with_this_function();
  23. #ifdef BOOST_PFR_USE_LEGACY_UNSAFE_DECLVAL_IMPLEMENTATION
  24. typename std::remove_reference<T>::type* ptr = nullptr;
  25. ptr += 42; // suppresses 'null pointer dereference' warnings
  26. return static_cast<T>(*ptr);
  27. #else
  28. // Looks like `static_cast<T>(*ptr)` to prvalue fails on clang in C++26.
  29. // If this new implementation does not work for some cases, please, fill a
  30. // bug report and feel free to
  31. // define BOOST_PFR_USE_LEGACY_UNSAFE_DECLVAL_IMPLEMENTATION.
  32. using func_ptr_t = T(*)();
  33. func_ptr_t ptr = nullptr;
  34. return ptr();
  35. #endif
  36. }
  37. }}} // namespace boost::pfr::detail
  38. #endif // BOOST_PFR_DETAIL_UNSAFE_DECLVAL_HPP