auto type用途

It is not uncommon to want to store the value of an expression in a variable. To
declare the variable, we have to know the type of that expression. When we write a
program, it can be surprisingly difficult—and sometimes even impossible—to
determine the type of an expression. Under the new standard, we can let the compiler
figure out the type for us by using the auto type specifier. Unlike type specifiers, such
as double, that name a specific type, auto tells the compiler to deduce the type
from the initializer.

auto item=val1+val2;

以下语句是错误的:

auto a=0,b=3.14;//变量a与b类型不一致

auto要求在同一行内定义的变量类型一致

auto通常忽略顶层const,而不忽略底层const,如:

const int ci=i,&cr=ci;
auto b=ci;//顶层const被忽略
auto c=cr;//cr是顶层constci的别名
auto e=&ci;//e是const对象的指针,e是底层const

若希望auto类型具有顶层const属性,则需要显示地声明:

const auto b=ci;//此时b具有顶层const属性
时间: 2024-07-29 13:16:33

auto type用途的相关文章

Auto type deducing

基本上和模板的类型推测是一样的 ,除了一种情况eg. 1 auto x=27 // x is int 2 const auto cx=x //cx is const int 3 const auto & rx=x // rx is referance of int //同样的对于右值引用 auto&& uref1 =x //uref1 is int and lvalue auto&& uref2 = rx //uref2 is int& and lvalue

【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

top-level const As we've seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to

《Effective Modern C++》读书笔记 Item 2 auto的类型推导

注意: 还要学习一个 ↑↑↑↑ 这样的方框里的片段完全不来自于原书,而是我自己的理解. Item 2 Understand auto type deduction - auto类型推导 在C++11之前,auto 关键字一直是用于声明自动储存类型的变量时使用的,基本上没有什么实际作用,地位和 export 关键字(用于向编译单元之外导出模板,也在C++11中被取消)类似. 在C++11中,auto 终于不再废材,终于具备了类似C#中 var 关键字的效果,可以自动推导出变量的类型,可以少打几个字

C++教程之auto关键字的使用

一.auto关键字的前世 从C语言开始,auto关键字就被当作是一个变量的存储类型修饰符,表示自动变量(局部变量).它不能被单独使用,否则编译器会给出警告. #include <stdio.h> int main() { int a = 123; auto int b = 234; auto c = 345; printf("a = %d, b = %d, c = %d\n", a, b, c); return 0; } 编译运行结果: $ gcc main.c main.

c++11: trailing return type in functions(函数返回类型后置)

In C++03, the return type of a function template cannot be generalized if the return type relies on those of the template arguments. Here is an example, mul(T a, T b) is a function template that calculates the product of a and b. Arguments a and b ar

《Effective Modern C++》翻译--条款3: 理解decltype

条款3:理解decltype decltype 是一个非常有趣的怪兽.如果提供了一个名字或是表达式,decltype关键字将会告诉你这个名字或这个表达式的类型.通常情况下,结果与你的期望吻合.然而有些时候,decltype产生的结果领你挠头,使你去翻阅参考书或在网上问答中寻求答案. 我们先从通常的情况开始-这里没有暗藏惊喜.联系到在模板类型推导和auto类型推导中发生了什么,decltype关键字就像鹦鹉学舌一样,对于变量名称或表达式类型的推导跟模板类型推导和auto类型推导没有任何变化: co

谭浩强《C++程序设计》

figcaption{text-align:center;margin:8px;color:#999;font-size:14px;display:block}.htmledit_views a>img{padding:1px;margin:1px;border:none;outline:#0782c1 solid 1px}.htmledit_views .code-featured{border:5px solid red}.htmledit_views .math-featured{padd

[C++] Variables and Basic Types

Getting Started compile C++ program source $ g++ -o prog grog1.cc run C++ program $ ./prog The library, iostream, define four IO object: cin, cout, cerr, clog. std::cout << "hello world" << std::endl; The result of the output operato

CSS3伪类实现动画旋转效果

一个简单的动画效果demo,keyframes为关键帧,图片贴在代码下方.利用了伪类实现css3动画效果,初学者可以看一下,恩.<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Keywords" content="关键词"> <meta name="D