在main
函數中,我創建了一個變量const int
指針,將其指定給由auto&
聲明的變量。然後使用decltype(x)
來檢查類型。我預計這種類型是const int*
。但是is_same
返回false
。什麼類型是auto&x = const int *?
int main()
{
int a = 10;
const int * cp_val= &a;
auto& x = cp_val;
bool is_const_int_ptr = std::is_same<decltype(x), const int *>::value; // returns 0
// *x = 100; // error: assignment of read-only location '* x'
}
但是,如果我添加下面的輔助函數:
#include <boost/type_index.hpp>
template<typename T>
void print_type(T)
{cout << "type T is: "<< boost::typeindex::type_id_with_cvr<T>().pretty_name()<< '\n';}
在主,我調用函數
print_type(x); // It returns int const*
我缺少的東西std::is_same
?