문제

if(typeid(int) == typeid(const int))
       cout << "Same types"<< endl;

PROGRAM OUTPUT:

Same types

am I missing something? these are not same types lol.

도움이 되었습니까?

해결책

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.

다른 팁

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";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top