Basic Knowledge
Basic Knowledge
Brief Introduction
一些基础知识点(八股)
C++关键字
decltype
类型推导关键字,它能在编译阶段分析表达式的类型。主要功能是获取表达式的类型,而且不会对表达式进行求值。其语法格式如下:
1
decltype(expression) var; // 用表达式的类型来声明变量
应用场景
lambda表达式
对于一些难以手动书写的类型,像 lambda 表达式的类型,
decltype可以直接获取。1 2
auto lambda = [](int x) { return x * 2; }; decltype(lambda) anotherLambda = lambda;
定义与表达式类型一致的变量
1 2
int x = 42; decltype(x) y = 10; // y 的类型是 int
泛型编程与模版元编程
在模板编程中,当函数返回值的类型依赖于模板参数时,
decltype就能发挥很大作用。1 2 3 4
template<typename T, typename U> auto add(T t, U u) -> decltype(t + u) { return t + u; }
这里借助
decltype推导出t + u的类型,以此作为函数的返回类型。
This post is licensed under CC BY 4.0 by the author.