auto在c ++ 11是静态类型或动态类型 [英] auto in c++11 is static typing or dynamic typing

查看:105
本文介绍了auto在c ++ 11是静态类型或动态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++ 11中的auto命令是静态绑定(打字)还是动态绑定?



如果我有这个代码

  auto x = 5; 

编译器会确定x是一个int还是将在运行时解析? / p>

此外,以下代码中x的类型是什么?

  auto x = 5,y = 4.5 


解决方案

称为类型推断。输入仍然是 static ,但是让编译器确定表达式的类型。



具体 auto x = 5; ,在编译时,翻译为 int x = 5; 。因为这个原因,你总是在初始化时提供一个明确的表达式。例如 auto x; 无法工作


Is auto command in c++11 is static binding(typing) or dynamic binding?

i.e. if i have this code

auto x = 5;

will the compiler decide that "x" is a int, or will it be descovered on runtime?

also, what will be the type of x in the following code?

auto x = 5, y = 4.5

解决方案

This is called type inference. The typing is still static, but you leave the compiler figure out what the type of an expression is.

Specifically auto x = 5;, at compiler time, is translated to int x = 5;. For this very reason you have always to provide an unambiguous expression at initialization. For example auto x; wouldn't work.

这篇关于auto在c ++ 11是静态类型或动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆