why typeid returns that int and const int are same types

前端 未结 2 1276
不知归路
不知归路 2020-12-06 10:58
if(typeid(int) == typeid(const int))
       cout << \"Same types\"<< endl;

PROGRAM OUTPUT:

Same

相关标签:
2条回答
  • 2020-12-06 11:17

    You probably want this instead:

    #include <type_traits>
    
    if (std::is_same<int, const int>::value)
        std::cout << "same types\n";
    else
        std::cout << "different types\n";
    
    0 讨论(0)
  • 2020-12-06 11:19

    They aren't the same type, but the typeid operator strips const and volatile.

    From section 5.2.8 [expr.typeid]:

    The top-level cv-qualifiers of the glvalue expression or the type-id that is the operand of typeid are always ignored.

    0 讨论(0)
提交回复
热议问题