c++ auto 属性
auto 指定符(C++11 起)
对于变量,指定其类型将从其初始化器自动推导而出。
对于函数,指定其返回类型是尾随的返回类型或将从其 return 语句推导出 (C++14 起)。
对于非类型模板形参,指定其类型将从参数推导出。 (C++17 起)
语法
| auto variable initializer | (1) | (C++11 起) | |||||||
| auto function -> return type | (2) | (C++11 起) | |||||||
| auto function | (3) | (C++14 起) | |||||||
| decltype(auto) variable initializer | (4) | (C++14 起) | |||||||
| decltype(auto) function | (5) | (C++14 起) | |||||||
auto :: |
(6) | (概念 TS) | |||||||
| cv(可选) auto ref(可选) parameter | (7) | (C++14 起) | |||||||
template < auto Parameter > |
(8) | (C++17 起) | |||||||
cv(可选) auto ref(可选) [ identifier-list ] initializer ; |
(9) | (C++17 起) | |||||||
解释
只要初始化器的类型被确定,则编译器会用来自函数调用的模板实参推导规则所决定的类型替换关键词 auto (细节见模板实参推导)。关键词 auto 可与修饰符组合,如 const 或 & ,它们将参与类型推导。例如,给出 const auto& i = expr; , i 的类型恰是虚构模板 template<class U> void f(const U& u) 中参数 u 的类型,倘若函数调用 f(expr) 被编译。从而, auto&& 可根据初始化器被推导成左值或是右值引用,这被用于基于范围的for循环。
若 auto 被用于声明多个变量,则推导类型必须相符。例如,声明 auto i = 0, d = 0.0; 为病式,而声明 auto i = 0, *p = &i; 为良式,这里 auto 被推导成int。
auto ,则其类型由对应使用参数推导出。注意
C++11 前, auto 拥有存储期指定符的语义。
不允许在一个声明中混合 auto 变量和函数,如 auto f() -> int, i = 0; 。
示例
#include <iostream>
#include <utility>
template<class T, class U>
auto add(T t, U u) { return t + u; } // 返回类型是 operator+(T, U) 的类型
// 在其所调用的函数返回引用的情况下
// 函数调用的完美转发必须用 decltype(auto)
template<class F, class... Args>
decltype(auto) PerfectForward(F fun, Args&&... args)
{
return fun(std::forward<Args>(args)...);
}
template<auto n> // C++17 auto 形参声明
auto f() -> std::pair<decltype(n), decltype(n)> // auto 不能从花括号初始化器列表推导
{
return {n, n};
}
int main()
{
auto a = 1 + 2; // a 的类型是 int
auto b = add(1, 1.2); // b 的类型是 double
static_assert(std::is_same_v<decltype(a), int>);
static_assert(std::is_same_v<decltype(b), double>);
decltype(auto) c1 = a; // c1 的类型是 int ,保有 a 的副本
decltype(auto) c2 = (a); // c2 的类型是 int& ,为 a 的别名
std::cout << "a, before modification through c2 = " << a << '\n';
++c2;
std::cout << "a, after modification through c2 = " << a << '\n';
auto [v, w] = f<0>(); // 结构化绑定声明
auto d = {1, 2}; // OK : d 的类型是 std::initializer_list<int>
auto n = {5}; // OK : n 的类型是 std::initializer_list<int>
// auto e{1, 2}; // C++17 起错误,之前为 std::initializer_list<int>
auto m{5}; // OK : C++17 起 m 的类型为 int ,之前为 initializer_list<int>
// decltype(auto) z = { 1, 2 } // 错误: {1, 2} 不是表达式
// auto 常用于无名类型,例如 lambda 表达式的类型
auto lambda = [](int x) { return x + 3; };
// auto int x; // 于 C++98 合法, C++11 起错误
// auto x; // 于 C 合法,于 C++ 错误
}
可能的输出:
a, before modification through c2 = 3
a, after modification through c2 = 4
c++ auto 属性的更多相关文章
- Hibernate配置文件的hbm2ddl.auto属性
今天遇到一个有意思的问题,我目前做的一个网站采用Spring MVC + Spring + Hibernate的架构,我通过页面插入了一些数据到数据库,可是每次重启tomcat之后,数据都莫名其妙地丢 ...
- 在页面中有overflow-y:auto属性的div,当出现滚动条,点击返回顶部按钮,内容回这个div最顶部
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css中元素的auto属性值是什么意思,比如margin:0 auto表示什么?
auto 你可以理解为一种 自动/自适应 的概念 比如 现在项目需要一个宽度为960px的整体布局居中 根据用户浏览器大小不同你将需要使用margin:0 auto;来实现. 无论用户浏览器宽度为多少 ...
- CSS中margin:auto什么意思?margin:auto属性的用法详解
我们都知道使用margin:auto可以让元素水平居中的.但你有没有想过使用margin:auto可以让元素水平居中的原因,要回答这个问题,我们首先需要看一下margin:auto的工作原理.auto ...
- Android Auto开发之一《开始学习Auto 》
共同学习,共同进步, 转载请注明出处.欢迎微信交流:sfssqs,申请注明"Android Car"字样 ================= =================== ...
- 实用的CSS3属性和使用技巧
CSS可以改进网站的设计并且开拓网站设计更多的可能性,可以令你的网页更具吸引力.对于前端开发者.网站设计师来说,掌握并熟练应用CSS是一项必不可少的技能. 下面列出了一些非常实用的CSS3属性和使用技 ...
- 关于extern和static关键字引出的一些关于作用域和链接属性和存储类型的问题
在进入正题前我们必须了解一些概念: 标识符:标识符不仅仅代表着变量的名字,main()函数的main也是一个标识符,这点很重要. 存储类型:即变量的存储位置及其生存周期:静态区:分为两块 .date ...
- css中盒子宽高的auto
CSS盒模型中,auto属性只适用于外margin和width,height,border和padding不适用.下面就来说说它的情况.这里所说的都是标准流盒子. 1.横向来说 (1)若设置width ...
- css中auto的用法
—什么是auto? +auto是自适应的意思,auto是很多尺寸值的默认值,也就是由浏览器自动计算. +块级元素中margin.border.padding以及content宽度之和构成父元素widt ...
随机推荐
- ssm配置文件叙述
spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的 ...
- Python学习第五堂课
Python学习第五堂课推荐电影:华尔街之狼 被拯救的姜哥 阿甘正传 辛德勒的名单 肖申克的救赎 上帝之城 焦土之城 绝美之城 #上节内容: 变量 if else 注释 # ""& ...
- 2018 ,请领取您Power BI 年终报告
Power BI365 3Jan 2019 新年已至,岁寒温暖! 为方便Power BI用户们能快速找到所需要的Power BI各类型文章,小悦将2018年Power BI的所有精彩文章按照各应用场景 ...
- AI五子棋需求规格说明书
AI-Gobang AI五子棋小程序 github地址:https://github.com/holidaysss/AI-Gobang 程序简介 AlphaGo Zero在世界舞台上取得的巨大成功体现 ...
- wxPython制作跑monkey工具(python3)-带事件百分比显示界面
一. wxPython制作跑monkey工具(python3)-带事件百分比显示界面 源代码 Run Monkey.py #!/usr/bin/env python import wx import ...
- linux下用命令安装node&pm2
我的安装环境是腾讯云centos7操作系统,并且将安装包下载到了/usr/local/src目录下 一.下载node安装包 1.wget https://npm.taobao.org/mirrors/ ...
- 阿里云 oss 图片上传解决方案 vue (web直传)
我们通过aliyun-oss-web这个npm去解决 该文章主要介绍如何获取 imgSignature 和 imgPolicy 这两个参数 首先下载 web直传的案例 : http://files.c ...
- npm install详解
package.json中dependencies和devDependencies的部分都会被安装,区别在于前者用于生产环境,后者用于开发环境-g 表示全局安装,通常用于安装脚手架等工具–save(- ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- Selenium3+python几种定位元素的方法
学习小结: 这里使用ChromeV73+web driver 2.46 #几种定位方式: #Autotest.py from selenium import webdriver from seleni ...