fake_object.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (c) 2023 Bela Schaum, X-Ryl669, Denis Mikhailov.
  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. // Initial implementation by Bela Schaum, https://github.com/schaumb
  6. // The way to make it union and UB free by X-Ryl669, https://github.com/X-Ryl669
  7. //
  8. #ifndef BOOST_PFR_DETAIL_FAKE_OBJECT_HPP
  9. #define BOOST_PFR_DETAIL_FAKE_OBJECT_HPP
  10. #pragma once
  11. #include <boost/pfr/detail/config.hpp>
  12. #ifdef __clang__
  13. # pragma clang diagnostic push
  14. # pragma clang diagnostic ignored "-Wundefined-internal"
  15. # pragma clang diagnostic ignored "-Wundefined-var-template"
  16. #elif defined(__GNUC__)
  17. # pragma GCC diagnostic push
  18. # pragma GCC diagnostic ignored "-Wundefined-var-template"
  19. #endif
  20. namespace boost { namespace pfr { namespace detail {
  21. // This class has external linkage while T has not sure.
  22. template <class T>
  23. struct wrapper {
  24. const T value;
  25. };
  26. // This variable servers as a link-time assert.
  27. // If linker requires it, then `fake_object()` is used at runtime.
  28. template <class T>
  29. extern const wrapper<T> do_not_use_PFR_with_local_types;
  30. // For returning non default constructible types, it's exclusively used in member name retrieval.
  31. //
  32. // Neither std::declval nor boost::pfr::detail::unsafe_declval are usable there.
  33. // This takes advantage of C++20 features, while boost::pfr::detail::unsafe_declval works
  34. // with the former standards.
  35. template <class T>
  36. constexpr const T& fake_object() noexcept {
  37. return do_not_use_PFR_with_local_types<T>.value;
  38. }
  39. }}} // namespace boost::pfr::detail
  40. #ifdef __clang__
  41. # pragma clang diagnostic push
  42. #elif defined(__GNUC__)
  43. # pragma GCC diagnostic pop
  44. #endif
  45. #endif // BOOST_PFR_DETAIL_FAKE_OBJECT_HPP