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;

}

 

 

 

 

 

 

 

 

posted on 2012-11-19 12:04  Torstan  阅读(310)  评论(0编辑  收藏  举报

导航