void_ptr_cast.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright Antony Polukhin, 2015-2024.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP
  8. #define BOOST_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <type_traits>
  14. #if defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 301)
  15. # pragma GCC system_header
  16. #endif
  17. namespace boost { namespace stacktrace { namespace detail {
  18. // GCC warns when reinterpret_cast between function pointer and object pointer occur.
  19. // This functionsuppress the warnings and ensures that such casts are safe.
  20. template <class To, class From>
  21. To void_ptr_cast(From* v) noexcept {
  22. static_assert(
  23. std::is_pointer<To>::value,
  24. "`void_ptr_cast` function must be used only for casting to or from void pointers."
  25. );
  26. static_assert(
  27. sizeof(From*) == sizeof(To),
  28. "Pointer to function and pointer to object differ in size on your platform."
  29. );
  30. return reinterpret_cast<To>(v);
  31. }
  32. }}} // boost::stacktrace::detail
  33. #endif // BOOST_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP