https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103600
Bug ID: 103600
Summary: Cannot use typeid result in constant expressions
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Keywords: rejects-valid
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
This should compile:
#include <typeinfo>
#if __cpp_lib_constexpr_typeinfo
constexpr bool b = typeid(int) == typeid(long);
#else
constexpr bool b = &typeid(int) == &typeid(long);
#endif
But GCC trunk (12.0.0 20211125) says:
ti.C:5:33: error: '(((const std::type_info*)(& _ZTIi)) == ((const
std::type_info*)(& _ZTIl)))' is not a constant expression
5 | constexpr bool b = &typeid(int) == &typeid(long);
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
This blocks
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1328r1.html (Making
std::type_info::operator== constexpr)
With the library changes in place compiling with -std=gnu++23 gives:
ti.C:3:32: in 'constexpr' expansion of '((const std::type_info*)(&
_ZTIi))->std::type_info::operator==(_ZTIl)'
ti.C:3:46: error: accessing value of '_ZTIi' through a 'const std::type_info'
glvalue in a constant expression
3 | constexpr bool b = typeid(int) == typeid(long);
| ^
Using clang++ -std=c++2b the same patched libstdc++ code gives:
ti.C:3:16: error: constexpr variable 'b' must be initialized by a constant
expression
constexpr bool b = typeid(int) == typeid(long);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/jwakely/gcc/latest/lib/gcc/x86_64-pc-linux-gnu/12.0.0/../../../../include/c++/12.0.0/typeinfo:195:9:
note: read of object 'typeid(int).__name' whose value is not known
if (__name == __arg.__name)
^
ti.C:3:32: note: in call to '&typeid(int)->operator==(typeid(long))'
constexpr bool b = typeid(int) == typeid(long);
^
Which presumably means that the RTTI for built-in types (defined in
libsupc++/fundamental_type_info.cc) needs to be made visible during constant
evaluation.