在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
?