c++c++17language-lawyerconstexprtypeid

Can pointers on std::type_info be compared for equality in constant expressions?


One can define a constexpr pointer on std::type_info object of any class T. Does the language allows one to compare such pointers for equality in compile-time?

For example:

#include <typeinfo>

template <typename T>
inline constexpr auto * pType = &typeid(T);

int main() {
    static_assert( pType<int> == pType<int> );
    static_assert( pType<int> != pType<char> );
} 

The question arises, since Clang accepts it, but GCC returns the error:

error: non-constant condition for static assertion
    8 |     static_assert( pType<int> != pType<char> );
      |                    ~~~~~~~~~~~^~~~~~~~~~~~~~
<source>:8:31: error: '(((const std::type_info*)(& _ZTIi)) != ((const std::type_info*)(& _ZTIc)))' is not a constant expression

Demo: https://gcc.godbolt.org/z/9broYrEn7


Solution

  • This is a GCC bug: 85428

    By the way, pType<int> == pType<int> is not always guaranteed.