std::decay_t是一个类型转换工具,它接受一个类型,并将其转换为对应的”衰减类型”。所谓”衰减类型”指的是将某个类型以如下方式处理得到的类型:

  • 对于数组或函数类型,将其转换为指针类型。
  • 对于const/volatile限定符和引用类型,去除这些修饰符,得到被修饰类型本身。
  • 对于非上述类型,保持其原样。

以下是一个使用std::decay_t的例子:

1
2
3
4
5
6
7
8
9
10
11
12
#include <type_traits>

int main() {
int arr[3] = {1, 2, 3};
using type1 = std::decay_t<decltype(arr)>; // type1会被推导为int*

const volatile double& ref = 42.0;
using type2 = std::decay_t<decltype(ref)>; // type2会被推导为double

struct S {};
using type3 = std::decay_t<S>; // type3会被推导为S
}