当 ParamType 既不是指针也不是引用时自动类型推导

auto type deduction when ParamType is neither a Pointer nor a Reference

我参考了 Scott Meyers "More Effective C++" 的自动类型推导。提到它与模板类型推导的工作方式相同,并且提到了 3 种情况。我的问题属于情况 3(当 ParamType 不是指针或引用时),但结果与描述不符。

#include <iostream>

int main (void)
{
   auto i = 2;
   const auto c = &i;

   *c = 4;

   std::cout << "i is " << i;
}

它应该像

template<typename T>
void f(const T param);

f(&i);   // int *

所以,这里的T应该匹配int *param的完整类型应该是const int *

但是,如上面的程序所示,c 不是 const int *,而是 int *

谁能解释一下我在这里遗漏了什么?

当你有

template<typename T>
void f(const T param);

T 是指针类型,您没有 const type *,而是 type * const,因为您正在制作 Tconst,而不是它指向什么。

也就是说

const auto c = &i;

是一个int * const,一个指向非常量整数的常量指针。