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
참고 적 최상위 CONST이 없습니다. –
아. 정확하게. 감사. const로 접두사를 붙일 수 있습니까? –
어떤 함수 호출을 참조하고 있습니까? – 0x499602D2