斗士狗 2009-01-30 10:06 采纳率: 0%
浏览 453
已采纳

为什么模板只能在头文件中实现?

Quote from The C++ standard library: a tutorial and handbook:

The only portable way of using templates at the moment is to implement them in header files by using inline functions.

Why is this?

(Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)

转载于:https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file

  • 写回答

14条回答 默认 最新

  • 喵-见缝插针 2009-01-30 10:26
    关注

    It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer.

    Anyway, the reason your code is failing is that, when instantiating a template, the compiler creates a new class with the given template argument. For example:

    template<typename T>
    struct Foo
    {
        T bar;
        void doSomething(T param) {/* do stuff using T */}
    };
    
    // somewhere in a .cpp
    Foo<int> f; 
    

    When reading this line, the compiler will create a new class (let's call it FooInt), which is equivalent to the following:

    struct FooInt
    {
        int bar;
        void doSomething(int param) {/* do stuff using int */}
    }
    

    Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int). If these implementations were not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.

    A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.

    // Foo.h
    template <typename T>
    struct Foo
    {
        void doSomething(T param);
    };
    
    #include "Foo.tpp"
    
    // Foo.tpp
    template <typename T>
    void Foo<T>::doSomething(T param)
    {
        //implementation
    }
    

    This way, implementation is still separated from declaration, but is accessible to the compiler.

    Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need:

    // Foo.h
    
    // no implementation
    template <typename T> struct Foo { ... };
    
    //----------------------------------------    
    // Foo.cpp
    
    // implementation of Foo's methods
    
    // explicit instantiations
    template class Foo<int>;
    template class Foo<float>;
    // You will only be able to use Foo with int or float
    

    If my explanation isn't clear enough, you can have a look at the C++ Super-FAQ on this subject.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?