居然还有这样使用的auto
2014-11-29 21:29
267 查看
今天学习了一下keyword,无意中发现了自己一直未曾接触到的auto
好吧,我又开始胡扯了!
automatic storage duration. (deprecated)
1) When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead. Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type if i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled.
2) In a function declaration, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type
syntax. (未曾发现这点)
1.大体意思讲解了auto的使用方式(除了函数),
2.(大学四年四级未过,今年我不考了)大体意思(在函数声明中,auto关键字不能用作函数返回的类型,它仅仅能够作为返回细节符号的一部分。。。)
本文参考:http://tool.oschina.net/apidocs/apidoc?api=cpp%2Fen%2Fcpp.html(auto)
好吧,我又开始胡扯了!
automatic storage duration. (deprecated)
1) When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead. Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type if i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled.
2) In a function declaration, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type
syntax. (未曾发现这点)
1.大体意思讲解了auto的使用方式(除了函数),
2.(大学四年四级未过,今年我不考了)大体意思(在函数声明中,auto关键字不能用作函数返回的类型,它仅仅能够作为返回细节符号的一部分。。。)
#include <iostream> #include <cmath> #include <typeinfo> template<class T,class U> auto add(T t,U u)->decltype(t+u)//一种符号的一部分,这样理解不会错把。。 { return t+u; } //来个变态的 auto get_fun(int argc)->double(*)(double)//返回一种函数,这种函数double x(double),因为要地址,所以使用* { switch (argc) { case 1: return std::fabs; case 2: return std::sin; default: return std::cos; } } int main() { auto a=add(1,2.54); std::cout<<typeid(a).name()<<std::endl; auto b=add('1','A'); std::cout<<typeid(b).name()<<std::endl; auto p=get_fun(2); std::cout<<p(3.14)<<std::endl;//cos(3.14) } //output /** d----------->double i------------>int(转换为int了) 0.00159265---------->==0 */
本文参考:http://tool.oschina.net/apidocs/apidoc?api=cpp%2Fen%2Fcpp.html(auto)
相关文章推荐
- 使用autolayout,设置子控件的宽度 与父视图的宽度成比例大小(这样类似可以设置多个按钮平均横屏排列)
- SSH集成项目,使用注解方式,竟然还有这样的问题!!
- 居然还有这样的地方
- 中搜这样的大公司居然还有这样的bug
- 居然还有这样的
- Vs2003使用时出现这个问题,正在郁闷中,网上找了好久,居然看到同样问题,马上拷贝来:)开心中
- Hotmail邮箱居然还有2G
- [随文杂记]windows的记事本想不到还有这样的BUG
- 安装了dns且可以正常工作为什么还有"没有可以使用的"DNS服务器"的错误
- Tomcat 暴露JSP文件内容(真想不到TOMCAT还有这样的漏洞)
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- 使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)
- 还有更好的写法吗(使用Timer来判断鼠标在控件上停留的时间)
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- 想不到我写的IDEA的帖子居然还有1K多的点击
- 使用 GNU autotools 改造一个软件项目
- 这两天好郁闷,用了三年的Delphi居然出现这样的界面提示~
- 真恶心,WORD中居然还有这事(转)
- 在使用SPS操作文件的时候碰到这样的问题
- spring 配置实例4(使用DefaultAdvisorAutoProxyCreator 自动代理,比较省时省力的做法)