C++模板的编译问题
来源:互联网 发布:程序员的数学线性代数 编辑:程序博客网 时间:2024/05/17 00:14
转载 http://blog.sina.com.cn/s/blog_5f8817250100i6xk.html
上一篇文章《C++编译器与链接器工作原理》
http://blog.sina.com.cn/s/blog_5f8817250100i3oz.html
template <class T>
void FunA(T t)
{
}
int main()
{
}
Test.h文件
template <class T>
class A
{
public:
}
Test.cpp文件
#include "Test.h"
template <class T>
void A<T>::FunA()
{
}
main.cpp文件
#include "Test.h"
int main()
{
}
1)类和其他类型全部放在头文件中,这些头文件具有.hpp(或者.H, .h, .hh, .hxx)扩展名。
2)对于全局变量和(非内联)函数,只有声明放在头文件中,而定义放在点C文件中,这些文件具有.cpp(或者.C, .c, .cc, .cxx)扩展名。
// basics/myfirst.hpp
#ifndef MYFIRST_HPP
#define MYFIRST_HPP
// declaration of template
template <typename T>
void print_typeof (T const&);
#endif // MYFIRST_HPP
print_typeof()声明了一个简单的辅助函数用来打印一些类型信息。函数的定义放在点C文件中:
// basics/myfirst.cpp
#include <iostream>
#include <typeinfo>
#include "myfirst.hpp"
// implementation/definition of template
template <typename T>
void print_typeof (T const& x)
{
}
// basics/myfirstmain.cpp
#include "myfirst.hpp"
// use of the template
int main()
{
}
// basics/myfirst2.hpp
#ifndef MYFIRST_HPP
#define MYFIRST_HPP
#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)
{
}
#endif // MYFIRST_HPP
这在实践中确实是一个问题,因为它增加了编译器在编译一个实际程序时所需的时间。我们因此会在以后的章节中验证其他一些可能的方法来解决这个问题。但无论如何,现实世界中的程序花一小时来编译链接已经是快的了(我们曾经遇到过花费数天时间来从源码编译的程序)。
- 【C++】模板的分类编译
- C++模板的编译问题
- C++模板的编译问题
- C++模板的编译问题
- C++模板的编译问题
- 《模板的分离编译问题》
- 模板的分离编译问题
- 模板的分离编译问题
- c.cpp混合编译的makefile模板
- 关于C++模板编译不了的问题
- 关于模板类的编译问题
- 关于模板的分离式编译问题
- C++模板类的编译问题
- 关于g++编译模板类的问题
- C++模板类的编译问题
- C++模板类的编译问题
- 类模板编译时遇到的问题
- 模板的声明和定义编译问题
- tomcat配置虚拟主机,免输入端口
- 柴静:只求了解与认识而已
- tomcat设置默认项目和端口
- ArrayList,Vector,LiskedList的区别
- 字符串分割 -- sh
- C++模板的编译问题
- nyoj77开灯问题
- 程序员的自我修养(1)——操作系统篇
- (五)Android仿微信—仿微信聊天界面,以及语音录制功能
- 跨平台移动开发phonegap/cordova 3.3全系列教程-图片上传
- media媒体查询的用法
- Android Spinner修改文字颜色
- Oracle学习——扫盲篇
- adb安装-linux