https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102258

--- Comment #1 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
Workaround for my use-case: https://godbolt.org/z/5Woe8dvan.
```C++
#include <cassert>
#include <concepts>
#include <type_traits>
#include <typeinfo>
#include <utility>
struct B { virtual ~B() = default; };
struct D : B { int x{1}; constexpr ~D() override { } };

template<class DerivedPointer, class Base>
  requires(
    std::is_pointer_v<DerivedPointer> and
    not std::is_same_v<std::remove_cv_t<std::remove_pointer_t<DerivedPointer>>,
std::remove_cv_t<Base>> and
    std::derived_from<std::remove_pointer_t<DerivedPointer>, Base>)
[[nodiscard]] constexpr auto dynamic_downcast(Base* b)
  -> decltype(&dynamic_cast<std::remove_pointer_t<DerivedPointer>&>(*b)) {
  if (b == nullptr) return nullptr;
  if (&typeid(*b) == &typeid(std::remove_pointer_t<DerivedPointer>&))
    return &static_cast<std::remove_pointer_t<DerivedPointer>&>(*b);
  throw std::bad_cast{};
}

int main() {
  []() consteval {
    B* b = new D;
    assert(dynamic_downcast<D*>(b)->x == 1);
    delete b;
  }();
}
```

Reply via email to