https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70197
Bug ID: 70197
Summary: dynamic_cast treated as constant expression
Product: gcc
Version: 6.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
According to [expr.const] of C14 the result of the dynamic_cast operator is not
a core constant expression and thus cannot be used in contexts where core
constant expressions are required such as in constexpr expressions or as
template arguments. Therefore, the program below is ill-formed and should be
rejected with an error (as it is for example by Clang), but it is accepted by
GCC 6 and prior.
$ cat v.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc
-B/home/msebor/build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic -o/dev/null
-xc++ v.c
struct A { virtual ~A (); } a;
struct B: A { } b;
constexpr A *p0 = dynamic_cast<A*>(&a);
constexpr B *p1 = dynamic_cast<B*>(&a);
constexpr B *p2 = dynamic_cast<B*>(&b);
template <class T, T*> struct S { };
S<A, dynamic_cast<A*>(&a)> s0;
S<B, dynamic_cast<B*>(&a)> s1;
S<B, dynamic_cast<B*>(&b)> s2;
v.c:5:38: warning: dynamic_cast of ‘A a’ to ‘struct B*’ can never succeed
constexpr B *p1 = dynamic_cast<B*>(&a);
^
v.c:10:25: warning: dynamic_cast of ‘A a’ to ‘struct B*’ can never succeed
S<B, dynamic_cast<B*>(&a)> s1;
^