auto关键字在推断类型时,如果没有引用符号,会忽略值类型的const修饰,而保留修饰指向对象的const,典型的就是指针
#include <iostream>
#include <boost/type_index.hpp>
int main() {
using boost::typeindex::type_id_with_cvr;
int a = 10;
int b = 20;
const int* p1 = &a;
p1 = &b; // right
*p1 = 30; // wrong
int* const p2 = &a;
p2 = &b; // wrong
*p2 = 40; // right
auto tp1 = p1;
tp1 = &b; // right
*tp1 = 30; // wrong
auto tp2 = p2;
tp2 = &b; // right
*tp2 = 40; // right
const int c = 10;
auto tc = c;
// 输出 int const * (等价于 const int *)
std::cout << type_id_with_cvr<decltype(tp1)>().pretty_name() << std::endl;
// 输出 int *
std::cout << type_id_with_cvr<decltype(tp2)>().pretty_name() << std::endl;
// 输出 int
std::cout << type_id_with_cvr<decltype(tc)>().pretty_name() << std::endl;
return 0;
}