🐭 auto 类型推导与模板类型推导

auto 类型推导与模板类型推导大致遵循相同的规则,它们之间的对应关系为 (auto <=> T):
代码示例 :

  1. #include <iostream>
  2. // template type deduction
  3. // case 1: param's type is a reference or pointer
  4. template <typename T> void f(T &param);
  5. template <typename T> void f(T *param);
  6. // case 2: param's type is a universal reference
  7. template <typename T> void f(T &&param);
  8. // case 3: param's type is a value type
  9. template <typename T> void f(T param);
  10. int main(void) {
  11. int x = 42;
  12. int &rx = x;
  13. const int cx = x;
  14. const int &crx = cx;
  15. // auto type deduction
  16. // case 1: param's type is a reference or pointer
  17. auto &param11 = rx; // param11 is `int &`
  18. auto &param12 = crx; // param12 is `const int&`
  19. auto *param13 = &x; // param13 is `int *`
  20. auto *param14 = &cx; // param14 is `const int *`
  21. // case 2: param's type is a universal reference
  22. auto &&param21 = x; // param21 is `int &`
  23. auto &&param22 = cx; // param22 is `const int&`
  24. auto &&param23 = std::move(x); // param23 is `int &&`
  25. auto &&param24 = std::move(cx); // param24 is `const int &&`
  26. // case 3: param's type is a value type
  27. const volatile int &cvrefx = x;
  28. const int *const cpcx = &x;
  29. int ax[2] = {x, x};
  30. auto param31 = cvrefx; // param31 is `int`
  31. auto param32 = cpcx; // param32 is `const int*`
  32. auto param33 = ax; // param33 is `int*`
  33. return 0;
  34. }

🐂 auto 类型推导与 std::initializer_list

auto 会将大括号初始化当成 std::initializer_list 来处理,并且有两种不同的初始化方式:

  1. 拷贝初始化
  2. 直接初始化,C++17 新增的规则
  3. 当 auto 作为函数返回值与 Labmda 形参时无法推导初始化列表

模板不会推导初始化列表,除非模板形参为初始化列表类型。 :::tips auto 作为函数返回值与 Labmda 形参时,实际上遵循的是模板类型推导规则 ::: 代码示例 :

  1. #include <initializer_list>
  2. template <typename T> void f(T){};
  3. template <typename T> void g(std::initializer_list<T>){};
  4. int main(void) {
  5. // case 1: auto deduction initializer list
  6. auto x11 = 27; // x11 is int
  7. auto x12(27); // x12 is int
  8. auto x13 = {27}; // copy-initialisation, x13 is std::initializer_list<int>
  9. auto x14{27}; // direct-initialisation (until C++17), x14 is int
  10. // case 2: auto deduction invalid case
  11. // auto x21 = {1, 2.0}; // error: conflicting types
  12. // auto x22{1, 2}; // error: not a single value
  13. // case 3: auto deduction for function return value
  14. // auto ints = []() { return {1, 2, 3}; }; // error: can not deduce initializer list
  15. // case 4: auto deduction for lambda function parameter
  16. auto printv = [](const auto &v) {};
  17. // printv({1, 2, 3}); // error: can not deduce initializer list
  18. // case 5: template deduction initializer list
  19. // f({1, 2, 3, 4, 5, 6}); // error: can not deduce initializer list
  20. auto x5 = {1, 2, 3, 4, 5, 6}; // std::initializer_list<int>
  21. f(x5); // work around
  22. g({1, 2, 3, 4, 5, 6});
  23. return 0;
  24. }

:::tips 虽然模板不能推导初始化列表,但是可以结合 auto 来间接支持,比如上面代码中的 x5 的使用 :::

🐯 总结

  1. auto 类型推导与模板类型推导大致遵循相同的规则
  2. auto 作为函数返回值与 Labmda 形参时,实际上遵循的是模板类型推导规则
  3. auto 类型推导与模板类型推导的唯一区别是 auto 作为变量使用时可以推导 std::initializer_list