谁能告诉我为什么通用引用会失去顶级简历的资格?我预计输出将在以下代码中的第二个和第三个函数调用中为 const 返回 true。
#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;
}
输出:
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