【发布时间】:2017-10-19 00:21:54
【问题描述】:
在main函数中,我创建了一个const int指针的变量,将它赋值给auto&声明的变量。然后使用decltype(x) 检查类型。我预计类型是const int*。但是is_same 返回false。
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 中遗漏了什么吗?
【问题讨论】:
标签: c++ c++11 auto type-deduction