2017-11-23 7 views
-1

std::is_same()typeid()에 대한 두 개의 테스트 케이스가 있습니다.왜 typeid가 true로 인쇄됩니까?

사례 1 : std::is_same()

#include <iostream> 
#include <type_traits> 
#include <cstdint> 

int main() 
{ 
    std::cout << std::boolalpha; 
    std::cout << std::is_same<int, volatile int>::value << '\n'; // false 
} 

출력은 :

false 

를 그 정확한 출력을 제공한다.

경우 2 : typeid()

#include <iostream> 
#include <cstdlib> 
using namespace std; 

#define CMP_TYPE(a, b) cout<<(typeid(a) == typeid(b)) << endl; 

int main() 
{ 
    cout << std::boolalpha; 
    CMP_TYPE(int, volatile int) 
} 

출력의 경우는 :

true 

왜 인쇄 진정한 유형 ID는 무엇입니까?

+3

HTTP : // EN 모든 경우에

는 이력서 - 예선은 내가이 일을 얻을 수 있다는 것을 의미합니다 (즉, typeid(T) == typeid(const T)입니다)

유형 ID에 의해 무시됩니다. cppreference.com/w/cpp/language/typeid – Mat

+2

Mat의 링크에서 : "모든 경우에 cv 한정자는'typeid' (즉,'typeid (T) == typeid (const T))'에 의해 무시됩니다. 이 동작이 예상됩니다. – Quentin

+3

"잘못"하지 않았습니다. 다르게 작동하도록 정의되었습니다. 질문을 다시 말하면 실제로 흥미로운 질문 일 수 있습니다. – StoryTeller

답변

2

CppReference.

#define TYPECMP(T, U) (typeid(T) == typeid(U)) 
assert(TYPECMP(int, const int)); 
assert(TYPECMP(int, volatile int)); 
assert(TYPECMP(int, const volatile int)); 
assert(TYPECMP(const int, volatile int)); 
+0

'constexpr' 값이 아니기 때문에 그럴 수 없습니다.) – Quentin

+0

@Quentin 어떻게 향상시킬 수 있습니까? 'static_assert'를'assert'로 대체하는 것이 효과가 있습니까? – iBug

+0

예. Protip : [온라인 컴파일러] (http://coliru.stacked-crooked.com/)를 사용하면 빠르게 테스트 할 수 있습니다 :) – Quentin