C++ Quiz68 of 173
Difficulty:
⬅️ Prev: 153Quiz ID: 157Next: 158 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>
#include <typeinfo>

struct A {};

int main()
{
    std::cout<< (&typeid(A) == &typeid(A));
}
Hint
  1. What is the value category of typeid result?
  2. Does all typeid(A) refer the same object?
Reveal answer

The program is unspecified / implementation defined

Explanation

§[expr.typeid]¶1:

The result of a typeid expression is an lvalue of static type const std::type_info (...).

and §[expr.unary.op]¶3:

The operand of the unary & operator shall be an lvalue of some type T. The result is a prvalue.

— (...)

— Otherwise, the result has type “pointer to T” and points to the designated object (...).

So we're comparing two pointers to const std::type_info.

There is no guarantee that the same std::type_info instance will be referred to by all evaluations of the typeid expression on the same type, although std::type_info::hash_code of those type_info objects would be identical, as would be their std::type_index.

(For more info on hash_code(), see §[type.info]¶6: "hash_code() (...) shall return the same value for any two type_info objects which compare equal".)

(For more info on type_index equality, see §[type.index.members] and §[type.info].)

⬅️ Prev: 153Quiz ID: 157Next: 158 ➡️