🐭 decltype 与 auto
decltype 能返回一个变量或者表达式的值类型,auto 可以推导一个变量或者表达式的值类型。其实编译器总是知道一个变量或者表达式的值类型,decltype 和 auto 是为了简化编码服务的,是给程序员使用的,本质上都是语法糖。
代码示例 :
#include <complex>#include <initializer_list>#include <iostream>void fun(int) {}int main(void) {using std::cout, std::is_same_v;std::cout.setf(std::ios_base::boolalpha);using T = std::complex<double>;T t00{};T &t01 = t00;T ta[2] = {{}, {}};// case 1: type specifier is a pointer or reference (not universal reference)auto *t11 = &t00; // pointerauto &t12 = t00; // lvalue referenceauto &&t13 = T{}; // rvalue referencecout << is_same_v<decltype(t11), T *> << '\n'; // truecout << is_same_v<decltype(t12), T &> << '\n'; // truecout << is_same_v<decltype(t13), T &&> << '\n'; // true// case 2: type specifier is a universal referenceauto &&t21 = t00; // lvalue referenceauto &&t22 = T{}; // rvalue referencecout << is_same_v<decltype(t21), T &> << '\n'; // truecout << is_same_v<decltype(t22), T &&> << '\n'; // true// case 3: type specifier is neither a pointer nor a referenceauto t3 = t00; // valuecout << is_same_v<decltype(t3), T> << '\n'; // true// case 4: array and functionauto t41 = ta; // by valueauto &t42 = ta; // by referencecout << is_same_v<decltype(t41), T *> << '\n'; // truecout << is_same_v<decltype(t42), T(&)[2]> << '\n'; // trueauto t43 = fun; // by valueauto &t44 = fun; // by referencecout << is_same_v<decltype(t43), void (*)(int)> << '\n'; // truecout << is_same_v<decltype(t44), void (&)(int)> << '\n'; // true// case 5: decltype autoauto t51 = t01; // auto by value, strip the reference-nessdecltype(auto) t52 = t01; // decltype(auto) by value, keeps the reference-nesscout << is_same_v<decltype(t51), T> << '\n'; // truecout << is_same_v<decltype(t52), T &> << '\n'; // true// case 6: initializer listauto t61 = {T{}}; // copy-initialisation, t61 is initializer_list<T>auto t62{T{}}; // direct-initialisation, t62 is Tcout << is_same_v<decltype(t61), std::initializer_list<T>> << '\n'; // truecout << is_same_v<decltype(t62), T> << '\n'; // truereturn 0;}
:::tips decltype(auto) 能根据 auto 的初始化物来推导类型,不受 auto 类型推导规则的影响 :::
🐂 decltype 的应用
一个函数的进化历程:
假定我们有一个需求,给定一个容器 c,返回其中索引为 i 的元素,暂不考虑错误处理。为了通用,我们可以设计以下四种形式的函数模板。这四种形式都能保持 c[i] 类型不变的返回给调用方,但在适用性方面它们之间是有差异的,注意其中的 decltype(auto) 的使用。
代码示例 :
#include <array>#include <iostream>#include <numeric>// access0 ~ access3 can keeps the c[i]'s type not changedtemplate <typename Container, typename Index>auto access0(Container &c, Index i) -> decltype(c[i]) {return c[i];}template <typename Container, typename Index>decltype(auto) access1(Container &c, Index i) {return c[i];}template <typename Container, typename Index>decltype(auto) access2(const Container &c, Index i) {return c[i];}template <typename Container, typename Index>decltype(auto) access3(Container &&c, Index i) {return std::forward<Container>(c)[i];}int main(void) {std::array<int, 4> arr11;std::iota(arr11.begin(), arr11.end(), 0);auto arr12{arr11};// access0 ~ access1 only accept lvaluestd::cout << (access0(arr11, 0)) << '\n'; // 0std::cout << (access1(arr11, 1)) << '\n'; // 1// access2 ~ access3 accept both lvalue and rvlauestd::cout << (access2(arr11, 2)) << '\n'; // 2std::cout << (access3(arr11, 3)) << '\n'; // 3std::cout << access2(std::move(arr11), 2) << '\n'; // 2std::cout << access3(std::move(arr12), 3) << '\n'; // 3return 0;}
:::tips 通用(转发)引用,配合完美转发,再配合 decltype(auto) 是一种常见的组合,很实用
常量左值引用 (const T & pc):
- 也是一种通用引用类型,能接收左值,右值,但是都转化为常量左值引用
- 当接收右值的时候,会延长对象的生命周期,因此,常量左值引用可以安全的保存临时对象
:::
🐯 decltype 的特殊情形
decltype 其实非常简单,绝大部分时候都能很好的工作,返回我们期望其返回的类型。decltype 只会作用于左值,当作用于左值名字时,会返回 T 类型,但是作用于非名字左值时,会返回 T& 类型。这都是一些不常见情形,了解即可。
代码示例 : ```cppinclude
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
cout << is_same_v
return 0; } ``` :::tips 对于函数的返回值,如果返回单个变量名字时,不要乱加小括号 :::
🐰 总结
- decltype 总是忠实的返回变量或者表达式的值类型
- decltype 作用于 T 类型的非左值名字表达式时,返回 T& 类型
- decltype(auto) 会返回 auto 初始化物的类型,相当于跳过了 auto 类型推导
