zoukankan      html  css  js  c++  java
  • 【c++ templates读书笔记】【5】模板实战

    1、模板声明和模板定义如果不在同一个文件中,在另一个文件中使用模板时会出现链接错误。

    例子:

    //Myfirst.h
    #ifndef MYFIRST_H
    #define MYFIRST_H
    
    #include<iostream>
    #include<typeinfo>
    using namespace std;
    
    template<typename T>
    void print_typeof(T const& x);
    
    #endif
    
    //Myfirst.cpp
    #include"Myfirst.h"
    #include<iostream>
    #include<typeinfo>
    using namespace std;
    
    template<typename T>
    void print_typof(T const& x){
    	cout << typeid(x).name() << endl;
    }
    
    //main.cpp
    #include"Myfirst.h"
    
    int main(){
    	double ice = 3.0;
    	print_typeof(ice);
    
    	system("pause");
    	return 0;
    }
    

    原因:函数模板print_typeof()的定义还没有被实例化。当实例化一个模板时,编译器必须知道应该实例化哪个定义及要基于哪个模板实参进行实例化。编译器在MyFirst.h文件中看到了模板的声明,但没有模板的定义,这样编译器就不能创建voidprint_typof(double const& x),但这时并不出错,因为编译器认为模板定义在其它文件中,并产生一个指向该定义的引用,让链接器利用该引用解决这个问题。

    2、解决以上问题可以有以下几种方法:

    2.1、包含模型:把模板的定义包含在声明模板的头文件里

    //Myfirst.h
    #ifndef MYFIRST_H
    #define MYFIRST_H
    
    #include<iostream>
    #include<typeinfo>
    using namespace std;
    
    template<typename T>
    void print_typeof(T const& x);
    
    template<typename T>
    void print_typeof(T const& x){
    	cout << typeid(x).name() << endl;
    }
    
    #endif
    //main.cpp
    #include"Myfirst.h"
    
    int main(){
    	double ice = 3.0;
    	print_typeof(ice);
    
    	system("pause");
    	return 0;
    }

    包含模型的缺点:a、增加了头文件的开销 b、大大增加了编译复杂程序所耗费的时间

    2.2、显示实例化

    在1的基础上,添加MyFirstinst.cpp

    //Myfirst.h
    #ifndef MYFIRST_H
    #define MYFIRST_H
    
    template<typename T>
    void print_typeof(T const& x);
    
    #endif
    //Myfirst.cpp
    #include"Myfirst.h"
    #include<iostream>
    #include<typeinfo>
    using namespace std;
    
    template<typename T>
    void print_typeof(T const& x){
    	cout << typeid(x).name() << endl;
    }
    //Myfirstinst.cpp
    #include"Myfirst.cpp"
    
    template void print_typeof<double>(double const& x);
    //main.cpp
    #include"Myfirst.h"
    #include<iostream>
    using namespace std;
    
    int main(){
    	double ice = 3.0;
    	print_typeof(ice);
    
    	system("pause");
    	return 0;
    }

    2.3、分离模型:在模板定义和声明前加上关键字export

    如函数模板声明:

    //Myfirst.h
    #ifndef MYFIRST_H
    #define MYFIRST_H
    
    export template<typename T>
    void print_typeof(T const& x);
    
    #endif

    但VS 2013编译器还不支持。



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Vue 中使用 viewerjs
    PS 给照片换背景
    HTML学习-1网页基础知识
    git使用
    Java读取XML配置文件
    Java是如何读到hbase-site.xml 的内容的
    HBASE count方法总结
    Getting Started with Java Development on Docker
    持续集成案例学习:Docker、Java与Maven
    利用MAVEN打包时,如何包含更多的资源文件
  • 原文地址:https://www.cnblogs.com/ruan875417/p/4921344.html
Copyright © 2011-2022 走看看