C++ template -- 实践1
来源:互联网 发布:淘宝图片保护惩罚 编辑:程序博客网 时间:2024/05/17 09:42
1. 例子<link error>
//myfirst.h
template <typename T>
void print_typeof (T const&);
//myfirst.cpp
#include <iostream>
#include <typeinfo>
#include "myfirst.hpp"
// implementation/definition of template
template <typename T>
void print_typeof (T const& x)
{
std::cout << typeid(x).name() << std::endl;
}
//myfirstmain.cpp
#include "myfirst.hpp"
// use of the template
int main()
{
double ice = 3.0;
print_typeof(ice); // call function template for type double
}
链接出错!!实例化时,编译器要知道哪个定义以及基于那个模版实参进行实例化,这个例子里两部分信息分开.
1. 调用时没有看到基于 double的实例化,它假设在某处提供这个定义,产生了一个指向该定义引用。
2. 编译myfirst.cpp,他没有指出,编译器要基于特定实参所包含模板定义进行实例化!
//解决方法...
//myfirst2.h
#include <iostream>
#include <typeinfo>
// declaration of template
template <typename T>
void print_typeof (T const&);
// implementation/definition of template
template <typename T>
void print_typeof (T const& x)
{
std::cout << typeid(x).name() << std::endl;
}
//myfirst2.cpp
#include "myfirst2.hpp"
// using of template
int main()
{
double ice = 3.0;
print_typeof(ice); // call function template for type double
}
缺点:包含头文件带来开销!增加了编译复杂程序的时间与消耗!
总结:非内联函数模板与内联函数宏区别
1. 调用位置不进行扩展,调用时进行实例化!可能导致编译器在不同文件产生两个一样的拷贝!
2. 编译器来解决这个问题!!!
- C++ template -- 实践1
- C++template
- C template
- C++Template
- C++ Template 实践草稿
- C++: template meta programming, emample 1
- C++ Template 实践草稿(two)
- c程序设计实践-1
- list c++template
- Queue-C++template
- Stack-c++template
- C++Template之Priority_queue
- [C++] Template练习
- eclipse-c-template
- eclipse-c-template
- C++template ;模板学习
- list c++template
- c plus template
- Java中文乱码问题的解决方案
- _EPROCESS数据结构
- 从PC客户端到无线客户端的转变
- uboot 的启动过程及工作原理 进入 C 代码部分
- 数据挖掘进行用户行为分析
- C++ template -- 实践1
- Cisco2600模拟器-路由设置
- 如何隐藏tabbar
- uboot lds 文件说明
- 猪八戒学做网站之HTML5篇(3):网页不是图片
- VS2005,在设计模式下对控件重命名(改名)
- 英语学习站点
- 为自己的应用程序设定开机自动启动
- JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决