2013-07-04 2 views
1

누구나 유니버셜 참조가 최상위 레벨의 자격을 잃어버린 이유를 말해 줄 수 있습니까? 다음 코드에서는 두 번째 및 세 번째 함수 호출에서 const에 대해 출력이 true를 반환 할 것으로 예상했습니다.범용 레퍼런스는 최상위 레벨의 cv 한정자를 무시합니다

#include <iostream> 
#include <type_traits> 

using namespace std; 

template<class T> 
void print(T const &value){ 
    cout << "Printing from const & method: " << value << endl; 
} 

template<class T> 
void print(T const *value){ 
    cout << "Printing from const * method: " << *value << endl; 
} 

template<class T> 
void f(T&& item){ 
    cout << "T is const: " << boolalpha << is_const<decltype(item)>::value << endl; 

    print(std::forward<T>(item)); 
} 


int main(){ 

    f(5); 

    const int a = 5; 
    f(a); 

    const int * const ptr = &a; 

    f(ptr); 

    return 0; 
} 

출력 : R. 마르틴는 지적

T is const: false 
Printing from const & method: 5 
T is const: false 
Printing from const & method: 5 
T is const: false 
Printing from const * method: 5 
+2

참고 적 최상위 CONST이 없습니다. –

+0

아. 정확하게. 감사. const로 접두사를 붙일 수 있습니까? –

+0

어떤 함수 호출을 참조하고 있습니까? – 0x499602D2

답변

4

, 참조는 최상위 CONST이 없습니다.

std::remove_reference을 사용할 수 있습니다, 낮은 수준의 CONST 다움을 확인하려면 :

cout << "T is const: " << boolalpha 
    << is_const<typename remove_reference<decltype(item)>::type>::value 
    << endl; 
+0

:) 모두에게 감사합니다. 굉장해. –

관련 문제