C++ 函数模板示例 2 (配合decltype)
来源:互联网 发布:mac 怎么读取exe 编辑:程序博客网 时间:2024/06/05 10:52
#include "stdafx.h"#include <iostream>#include <iomanip>#include <string.h>#include <new>using std::cout;using std::cin;using std::endl;using std::setw;template<class T1, class T2> auto product(T1 v1[], T2 v2[], size_t count) -> decltype(v1[0]*v2[0]){decltype(v1[0]*v2[0]) sum(0);for(size_t i=0;i<count;i++) sum += v1[i]*v2[i];return sum;}int main(int argc,_TCHAR* argv[]){double x[] = {100.5,99.5,88.7,77.8};short y[] = {3,4,5,6};long z[] = {11L,22L,33L,44L};size_t n=4;cout <<"result type is " <<typeid(product(x,y,n)).name()<<endl;cout<<"result is "<<product(x,y,n)<<endl;cout <<"result type is " <<typeid(product(z,y,n)).name()<<endl;cout<<"result is "<<product(z,y,n)<<endl;return 0;}
</pre><pre name="code" class="cpp">上面代码的运行结果是:
更多完整的示例,可以参见《Visual C++ 2012入门经典》一书的212页,6.8节:使用函数的示例。
0 0
- C++ 函数模板示例 2 (配合decltype)
- 模板函数简单示例(C++)
- 函数模板和关键字decltype用法的注记
- C++函数模板示例
- 内联函数,引用变量,按引用传递参数,默认参数,函数重载,函数模板,函数模板具体化,decltype
- JSTL自定义函数,配合c标签使用
- C++程序--函数模板示例
- C++ 类模板作为函数模板示例
- C/C++_log2000_函数模板与类模板笔记2
- 【c/c++】函数模板和模板函数
- 函数指针类型与decltype
- decltype用于函数指针类型
- [C++] decltype类型指示符
- Modern C++(三)decltype关键字
- decltype
- decltype
- decltype
- C++ 模板 decltype关键字 RTTI机制
- NSIS脚本详解
- dom
- 用DIV做的页面在IE中不能居中,火狐中可以
- WSF插件
- 微信相关
- C++ 函数模板示例 2 (配合decltype)
- 新锐房地产销售管理系统(部分流程)技术解析(四) 销售管理_客户查询
- opencv透视变换GetPerspectiveTransform的总结
- Mac中使用eclipse做java项目,使用Runtime执行命令行的命令报错解决方法
- Android 事件分发机制详解(1)-View
- BestCoder Round #43
- web.xml 配置中classpath: 与classpath*:的区别
- eclipse扩展自定义工具栏按钮
- std::vector