🐭 decltype 与 auto

decltype 能返回一个变量或者表达式的值类型,auto 可以推导一个变量或者表达式的值类型。其实编译器总是知道一个变量或者表达式的值类型,decltype 和 auto 是为了简化编码服务的,是给程序员使用的,本质上都是语法糖。
代码示例 :

  1. #include <complex>
  2. #include <initializer_list>
  3. #include <iostream>
  4. void fun(int) {}
  5. int main(void) {
  6. using std::cout, std::is_same_v;
  7. std::cout.setf(std::ios_base::boolalpha);
  8. using T = std::complex<double>;
  9. T t00{};
  10. T &t01 = t00;
  11. T ta[2] = {{}, {}};
  12. // case 1: type specifier is a pointer or reference (not universal reference)
  13. auto *t11 = &t00; // pointer
  14. auto &t12 = t00; // lvalue reference
  15. auto &&t13 = T{}; // rvalue reference
  16. cout << is_same_v<decltype(t11), T *> << '\n'; // true
  17. cout << is_same_v<decltype(t12), T &> << '\n'; // true
  18. cout << is_same_v<decltype(t13), T &&> << '\n'; // true
  19. // case 2: type specifier is a universal reference
  20. auto &&t21 = t00; // lvalue reference
  21. auto &&t22 = T{}; // rvalue reference
  22. cout << is_same_v<decltype(t21), T &> << '\n'; // true
  23. cout << is_same_v<decltype(t22), T &&> << '\n'; // true
  24. // case 3: type specifier is neither a pointer nor a reference
  25. auto t3 = t00; // value
  26. cout << is_same_v<decltype(t3), T> << '\n'; // true
  27. // case 4: array and function
  28. auto t41 = ta; // by value
  29. auto &t42 = ta; // by reference
  30. cout << is_same_v<decltype(t41), T *> << '\n'; // true
  31. cout << is_same_v<decltype(t42), T(&)[2]> << '\n'; // true
  32. auto t43 = fun; // by value
  33. auto &t44 = fun; // by reference
  34. cout << is_same_v<decltype(t43), void (*)(int)> << '\n'; // true
  35. cout << is_same_v<decltype(t44), void (&)(int)> << '\n'; // true
  36. // case 5: decltype auto
  37. auto t51 = t01; // auto by value, strip the reference-ness
  38. decltype(auto) t52 = t01; // decltype(auto) by value, keeps the reference-ness
  39. cout << is_same_v<decltype(t51), T> << '\n'; // true
  40. cout << is_same_v<decltype(t52), T &> << '\n'; // true
  41. // case 6: initializer list
  42. auto t61 = {T{}}; // copy-initialisation, t61 is initializer_list<T>
  43. auto t62{T{}}; // direct-initialisation, t62 is T
  44. cout << is_same_v<decltype(t61), std::initializer_list<T>> << '\n'; // true
  45. cout << is_same_v<decltype(t62), T> << '\n'; // true
  46. return 0;
  47. }

:::tips decltype(auto) 能根据 auto 的初始化物来推导类型,不受 auto 类型推导规则的影响 :::

🐂 decltype 的应用

一个函数的进化历程:
假定我们有一个需求,给定一个容器 c,返回其中索引为 i 的元素,暂不考虑错误处理。为了通用,我们可以设计以下四种形式的函数模板。这四种形式都能保持 c[i] 类型不变的返回给调用方,但在适用性方面它们之间是有差异的,注意其中的 decltype(auto) 的使用。
代码示例 :

  1. #include <array>
  2. #include <iostream>
  3. #include <numeric>
  4. // access0 ~ access3 can keeps the c[i]'s type not changed
  5. template <typename Container, typename Index>
  6. auto access0(Container &c, Index i) -> decltype(c[i]) {
  7. return c[i];
  8. }
  9. template <typename Container, typename Index>
  10. decltype(auto) access1(Container &c, Index i) {
  11. return c[i];
  12. }
  13. template <typename Container, typename Index>
  14. decltype(auto) access2(const Container &c, Index i) {
  15. return c[i];
  16. }
  17. template <typename Container, typename Index>
  18. decltype(auto) access3(Container &&c, Index i) {
  19. return std::forward<Container>(c)[i];
  20. }
  21. int main(void) {
  22. std::array<int, 4> arr11;
  23. std::iota(arr11.begin(), arr11.end(), 0);
  24. auto arr12{arr11};
  25. // access0 ~ access1 only accept lvalue
  26. std::cout << (access0(arr11, 0)) << '\n'; // 0
  27. std::cout << (access1(arr11, 1)) << '\n'; // 1
  28. // access2 ~ access3 accept both lvalue and rvlaue
  29. std::cout << (access2(arr11, 2)) << '\n'; // 2
  30. std::cout << (access3(arr11, 3)) << '\n'; // 3
  31. std::cout << access2(std::move(arr11), 2) << '\n'; // 2
  32. std::cout << access3(std::move(arr12), 3) << '\n'; // 3
  33. return 0;
  34. }

:::tips 通用(转发)引用,配合完美转发,再配合 decltype(auto) 是一种常见的组合,很实用

常量左值引用 (const T & pc):

  1. 也是一种通用引用类型,能接收左值,右值,但是都转化为常量左值引用
  2. 当接收右值的时候,会延长对象的生命周期,因此,常量左值引用可以安全的保存临时对象 :::

    🐯 decltype 的特殊情形

    decltype 其实非常简单,绝大部分时候都能很好的工作,返回我们期望其返回的类型。decltype 只会作用于左值,当作用于左值名字时,会返回 T 类型,但是作用于非名字左值时,会返回 T& 类型。这都是一些不常见情形,了解即可。
    代码示例 : ```cpp

    include

decltype(auto) T1() { unsigned int x = 1u; return x; };

decltype(auto) T2() { unsigned int x = 1u; return (x); };

int main(void) { using std::cout, std::is_same_v; cout.setf(std::ios_base::boolalpha);

// lvalue expression other than a name has type T, // decltype will reports that type as T& int x = 0; cout << is_same_v << ‘\n’; // true cout << is_same_v << ‘\n’; // true

cout << is_same_v << ‘\n’; // true cout << is_same_v << ‘\n’; // true

return 0; } ``` :::tips 对于函数的返回值,如果返回单个变量名字时,不要乱加小括号 :::

🐰 总结

  1. decltype 总是忠实的返回变量或者表达式的值类型
  2. decltype 作用于 T 类型的非左值名字表达式时,返回 T& 类型
  3. decltype(auto) 会返回 auto 初始化物的类型,相当于跳过了 auto 类型推导