Trailing return types
Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types:
C++03:
int func(int i, int j);
C++11可以写成:
auto func(int i, int j) -> int;
最直观感受就是,函数返回类型声明后置.
新的声明方式配合模版,可以使编译器自动推导模版函数的返回类型,使模版函数更泛化,例如:
C++03:
template<typename C1, typename C2, typename Ret>
Ret func(C1 i, C2 j){ return i + j;}
当你这样调用func(1, 2.0);编译器会告诉你,无法推导模版参数Ret.
调用函数时必须指定模版参数func<int, double, double>(1, 2.0);
这种声明方式即使在C++11,也无法配合decltype来推导,例如:decltype(i + j) func(C1 i, C2 j);因为decltype时,i j都还没有声明,为了解决这个问题,C++11引入了Trailing return types,看看怎么解决:
C++11:
template<typename C1 , typename C2 >
auto func(C1 i, C2 j)->decltype(i + j){ return i + j; }
把函数返回类型声明后置与函数形参声明,就可以启动decltype了。
明显地,使用Trailing return types的模版函数更泛用,因为函数所有的类型都能通过编译器自动推导,无需在源代码中显式指定。
Trailing return types另一个好处就是增强代码可读性:
C++03:
template <class T> class tmp
{
public:
int i;
};
tmp<int> (*(*foo())())()
{
return 0;
}
知道foo的返回类型是什么吗?再看
C++11:
template <class T> class tmp
{
public:
int i;
};
auto foo()->auto(*)()->tmp<int>(*)()
{
return 0;
}
这样应该清晰了吧,foo返回的是一个函数指针,这个函数指针的返回类型是tmp<int>(*)()函数指针.
参考
https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/introduction_to_the_c_11_feature_trailing_return_types?lang=en
Trailing return types的更多相关文章
- 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 ...
- Async Return Types
Async methods have three possible return types: Task<TResult>, Task, and void. The Task<TRe ...
- The return types for the following stored procedures could not be detected
1.使用dbml映射数据库,添加存储过程到dbml文件时报错. 2.原因:存储过程中使用了临时表 3.解决方案 3.1 通过自定义表值变量实现 Ex: DECLARE @TempTable TABLE ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- iOS:消除项目中警告
引言: 在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能执行之类的警告. 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!! 首先学会基本的语句: ...
- 【转】clang warning 警告清单(备查,建议直接command + F 速查 )
Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belon ...
- 使用#pragma阻止一些warnings
这篇博客的内容都是记的网上的.是流水账.只是记录下来以便日后之有,避免每次重新google. #pragma除了可以用来把不同功能的代码进行分隔组织外还可以用来disable一些warnings.这在 ...
- C++11 能好怎?
0. 摘要 近期读了一些关于C++11标准的材料. 本篇博客将从新标准的优点.与旧版本的区别和使用方法三个角度,大致介绍我对C++11的认识. C++11标准,原名C++0x, 是03版旧标准的更新. ...
- iOS -- warnings
Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte t ...
随机推荐
- jQuery滑动并响应事件
jQuery滑动并打开指定页面: <!DOCTYPE html> <html> <head> <script src="http://code.jq ...
- C# 文件粉碎
>文件粉碎的流程 填充文件 更改最后使用/创建时间 更改名称 将大小改为 0 kb 删除 using System; using System.Collections.Generic; usin ...
- ASP.NET MVC——Controller的激活
Controller的激活是根据在路由过程得到的Controller名称来创建对应的Controller对象.相关类如图: Controller激活的过程可通过如下序列图表示: 代码示例如下: str ...
- mvc5 + ef6 + autofac搭建项目(三)
前面已经基本完成了框架的搭建,后面就是实现了,后面主要说下前端的东西bootstrap的使用和相关插件. 看图: 实现比较简单,在主页面只引入共用部分的 js等相关包,毕竟不是所有页面都需要列表以及其 ...
- google code 上传源码
在使用google code 的时候 做个备份, git clone https://wushuangzilong@code.google.com/p/maplebanana-proxy/ git c ...
- 使用Delphi读取网络上的文本文件,html文件
使用Delphi读取网络上的txt和html文件 可以使用两种方法: 1.下载文件,然后进行读取 下载文件的Delphi代码可以参考: http://www.delphibbs.com/delphib ...
- Jsoup解析Html教程
Jsoup应该说是最简单快速的Html解析程序了,完善的API以及与JS类似的操作方式,为Java的Html解析带来极大的方便,结合多线程适合做一些网络数据的抓取,本文从一下几个方面介绍一下,篇幅有限 ...
- .NET 设计模式之简单工厂模式(二)
1:建立接口 namespace Factory { public interface IPerson { } } 2:建立Worker.Student来继承IPerson接口 namespace F ...
- tomcat上servlet程序的配置与处理servlet请求过程
手动配置: tomcat服务器下web项目的基本目录结构 |-tomcat根目录 |-webapps |-WebRoot : web应用的根目录 |-静态资源(html+css+js+image+ve ...
- java_设计模式_组合模式_Composite Pattern(2016-08-12)
概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...