public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103600] New: Cannot use typeid result in constant expressions
@ 2021-12-07  9:38 redi at gcc dot gnu.org
  2021-12-07  9:51 ` [Bug c++/103600] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-07  9:38 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2022-11-03 13:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07  9:38 [Bug c++/103600] New: Cannot use typeid result in constant expressions redi at gcc dot gnu.org
2021-12-07  9:51 ` [Bug c++/103600] " pinskia at gcc dot gnu.org
2021-12-07  9:52 ` pinskia at gcc dot gnu.org
2021-12-07 10:01 ` redi at gcc dot gnu.org
2021-12-07 13:23 ` jakub at gcc dot gnu.org
2021-12-07 13:47 ` redi at gcc dot gnu.org
2021-12-07 16:44 ` jakub at gcc dot gnu.org
2021-12-08  0:30 ` redi at gcc dot gnu.org
2022-01-03 10:24 ` cvs-commit at gcc dot gnu.org
2022-01-03 10:34 ` jakub at gcc dot gnu.org
2022-01-06 14:49 ` ppalka at gcc dot gnu.org
2022-11-03 13:29 ` ppalka 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).