当前位置: 首页 > news >正文

auto和decltype的区别

auto 让编译器推导变量类型

基本用法

auto 让编译器根据**初始化表达式**自动推导变量类型。

auto x = 5; // x 被推导为 int auto y = 3.14; // y 被推导为 double auto name = "hello"; // name 被推导为 const char* std::vector<int> vec = {1, 2, 3}; auto it = vec.begin(); // it 被推导为 std::vector<int>::iterator

auto 的推导规则

auto 遵循**模板参数推导规则**

const int a = 42; auto b = a; // b 是 int (const 和 & 被丢弃) auto& c = a; // c 是 const int& (引用和 const 保留) int arr[10] = {}; auto arr1 = arr; // arr1 是 int* (数组退化为指针) auto& arr2 = arr; // arr2 是 int(&)[10] (数组引用) const char* const str = "hello"; auto s1 = str; // s1 是 const char* (顶层const被丢弃)

auto在函数返回值中 (C++14)

C++14 允许函数返回值使用 auto 推导

auto add(int a, int b) { return a + b; // 返回类型推导为 int } auto getVector() { return std::vector<int>{1, 2, 3}; // 返回类型推导为 std::vector<int> }

auto 在 lambda 表达式中

C++14: lambda 参数可以使用 auto

auto lambda = [](auto x, auto y) { return x + y; }; std::cout << lambda(1, 2.5); // 3.5

decltype的推导规则

decltype有明确的规则:

1. 如果表达式是**变量名** → 返回该变量的声明类型(包括 const 和引用)

2. 如果表达式是**左值** → 返回 `T&`

3. 如果表达式是**将亡值** → 返回 `T&&`

4. 如果表达式是**纯右值** → 返回 `T`

int x = 0; const int cx = 1; int& rx = x; decltype(x) a; // 规则1: int decltype(cx) b = 2; // 规则1: const int decltype(rx) c = x; // 规则1: int& decltype(x + 0) d; // 规则4: int (x+0是右值) decltype((x)) e = x;// 规则2: int& ((x)是左值表达式) decltype(std::move(x)) f = 0; // 规则3: int&&

auto vs decltype的关键区别

推导方式的区别

const int x = 42; const int& rx = x; // auto: 丢弃 const 和引用 (除非显式指定) auto a1 = x; // int auto a2 = rx; // int // decltype: 保留完整类型信息 decltype(x) b1; // const int (错误,需要初始化) decltype(rx) b2; // const int& (错误,需要初始化) // auto 可以通过 & 和 const 来保留信息 auto& a3 = rx; // const int& const auto a4 = x; // const int

在返回值类型推导中的协同使用 (C++14)

// decltype(auto) - 用 decltype 的规则推导 auto

int x = 10; const int& getRef() { return x; } auto a = getRef(); // a 是 int (值类型) decltype(auto) b = getRef(); // b 是 const int& (保持原样)

在函数模板中特别有用

template<typename T1, typename T2> auto add(T1 a, T2 b) -> decltype(a + b) { // 尾置返回类型 return a + b; }
http://www.jsqmd.com/news/650877/

相关文章:

  • 【人工智能】Deepseek 专家模式 与 快速模式的差别?
  • 2小时完成本地部署OpenClaw新手友好!Cherry Studio+Ollama Cloud每周可免费使用约5次OpenClaw任务
  • 为什么已提交的数据一定存在于多数节点
  • 系统恢复与磁盘克隆:Rescuezilla 完全使用指南
  • 终极指南:如何免费延长JetBrains IDE试用期的完整解决方案
  • [ARC147F] Again ABC String 题解
  • 如何快速上手TimesFM:谷歌时间序列基础模型的完整实战指南
  • Harbor 2.8+ 弃用ChartMuseum后,如何用OCI规范管理Helm Charts?
  • 专业术语统计报告_基于复杂适应系统理论的多能源电力系统电源优化规划研究
  • AD9280 ADC模块原理图设计,已量产
  • 2026 托福机构排名 TOP5|大学生在职人士首选指南 - 速递信息
  • 2025最权威的六大降重复率工具推荐
  • 2026一体化预制泵站采购指南:行业头部三大厂商全方位横向评测 - 泵站报价15613348888
  • 微信小程序全局音频管理实战:防止创建多个InnerAudioContext实例
  • 大模型应用开发实战(13)——多 Agent 真的有必要吗?LangGraph 背后的分工逻辑拆解
  • 探索Intel NPU加速库:解锁AI硬件潜能的三步实战指南
  • 【算法刷题指南】从零开始的LeetCode系统训练(持续更新 分类索引)
  • OpenClaw飞书消息发送图片的坑:filePath 路径导致的显示差异
  • Linux 帮助手册与用户管理完全指南
  • 离心泵CAE_2_ICEM结构化网格划分实战
  • 5分钟搞定!Docker快速部署MQTT服务mosquitto(附手机APP测试指南)
  • 就在2月5日!维普系统全面升级:查重库与AI算法双重施压,2026毕业季保姆级通关指南
  • flutter--基础环境安装
  • 宁夏卷帘门加工维修找哪家?首选银川开源门业,承接各类卷帘门加工和维修,十年老厂,正规靠谱有实力,全区域上门服务 - 宁夏壹山网络
  • 08. Python进阶之路:深度解析递归、推导式、生成器与模块化编程
  • 从GAN到U-Net:实战中PyTorch转置卷积的参数配置与避坑指南
  • 永磁体温度稳定性优化:从剩磁温度系数到材料改性策略
  • 告别虚拟机!用ZYNQ7000和PYNQ 2.6.0打造一个能实时识别人脸的“智能摄像头”
  • Image Signal Processing(ISP)-第二章-从Bayer到RGB:Demosaic算法详解与BMP编码实战
  • 收官篇 —— 从会做事,到把事做对