zoukankan      html  css  js  c++  java
  • explicit instantiations in template class/function

    1. explicit instantiation of template function

    In a.h, an interface of template function is declared, and the implementation of the template function is defined in a.cc.

    In a.cc, we should add code to specify certain type of instatiations of the function so that the instantiation can be complied in final object file. For example,

    in a.h, template <class T> int f(T & t);

    in a.cpp, implemtation of f, and some instantiation.

    template int f(int & t);

    template int f(double & t);

    2. explicit instantiations of template class

    3. specification of static member in class 

    For example, in a.h, 

    template <typename T>

    class Obj {

      public:

        static Image<T>* getImage(int w, int h);

      private:

               Obj();

        static Mutex d_mutex;

    };

     In a.cpp,

    //explicit instantiation

    template class Obj::getImage<int>;

    template class Obj::getImage<unsigned int>;

    //specification of static member

    template <> Mutex Obj<int>::d_mutex;

    template <> Mutex Obj<unsigned int>::d_mutex;

    template<typename T>

    Image<T>* Obj::getImage(int w, int h)

    {

    static Image<T>* image;

    ...

    return image;

    }

  • 相关阅读:
    input 蓝边
    4.【ac自动机】模式串匹配
    3.【二叉树】最近公共祖先
    2.【动态规划】最长回文子串
    1. 【线段树】
    DbUtil
    oauth2
    cas
    Spring-security-web
    JSON Web Tokens
  • 原文地址:https://www.cnblogs.com/Torstan/p/2777042.html
Copyright © 2011-2022 走看看