public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102258] New: dynamic_cast to derived type fails during constant evaluation
@ 2021-09-09 14:31 johelegp at gmail dot com
  2021-09-09 17:09 ` [Bug c++/102258] " johelegp at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: johelegp at gmail dot com @ 2021-09-09 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102258
           Summary: dynamic_cast to derived type fails during constant
                    evaluation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johelegp at gmail dot com
                CC: johelegp at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/dsEzaxW47.
```C++
struct B { virtual ~B() = default; };
struct D : B { constexpr ~D() override { } };
int main() {
  []() consteval {
    B* b = new D;
    dynamic_cast<D&>(*b);
    delete b;
  }();
}
```
```
<source>: In function 'int main()':
<source>:8:4:   in 'constexpr' expansion of '<lambda closure
object>main()::<lambda()>().main()::<lambda()>()'
<source>:6:5: error: reference 'dynamic_cast' failed
    6 |     dynamic_cast<D&>(*b);
      |     ^~~~~~~~~~~~~~~~~~~~
<source>:6:5: note: dynamic type 'D [1]' of its operand does not have a base
class of type 'D'
Compiler returned: 1
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/102258] dynamic_cast to derived type fails during constant evaluation
  2021-09-09 14:31 [Bug c++/102258] New: dynamic_cast to derived type fails during constant evaluation johelegp at gmail dot com
@ 2021-09-09 17:09 ` johelegp at gmail dot com
  2021-09-09 19:40 ` johelegp at gmail dot com
  2022-10-22 22:42 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: johelegp at gmail dot com @ 2021-09-09 17:09 UTC (permalink / raw)
  To: gcc-bugs

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;
  }();
}
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/102258] dynamic_cast to derived type fails during constant evaluation
  2021-09-09 14:31 [Bug c++/102258] New: dynamic_cast to derived type fails during constant evaluation johelegp at gmail dot com
  2021-09-09 17:09 ` [Bug c++/102258] " johelegp at gmail dot com
@ 2021-09-09 19:40 ` johelegp at gmail dot com
  2022-10-22 22:42 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: johelegp at gmail dot com @ 2021-09-09 19:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
That still doesn't work because you can't compare (the addresses of) different
`std::type_info` objects.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/102258] dynamic_cast to derived type fails during constant evaluation
  2021-09-09 14:31 [Bug c++/102258] New: dynamic_cast to derived type fails during constant evaluation johelegp at gmail dot com
  2021-09-09 17:09 ` [Bug c++/102258] " johelegp at gmail dot com
  2021-09-09 19:40 ` johelegp at gmail dot com
@ 2022-10-22 22:42 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=106107
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-10-22

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-22 22:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-09 14:31 [Bug c++/102258] New: dynamic_cast to derived type fails during constant evaluation johelegp at gmail dot com
2021-09-09 17:09 ` [Bug c++/102258] " johelegp at gmail dot com
2021-09-09 19:40 ` johelegp at gmail dot com
2022-10-22 22:42 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).