A problem about explicit instantiation of class template

Discussion in 'C++' started by yuanhp_china@hotmail.com, Jan 1, 2008.

  1. Guest

    I define a class in A.h:

    template <class T> class A{

    public:
    void get_elem( const T&) ;
    };



    Then I give the implementation in A.cpp (and do an explicit
    instantiation ):

    template<class T> void A::get_elem( const T&)
    {...}

    template class A<int>; //explicit instantiation




    I have another two files: main.cpp and B.cpp. I would like to reuse
    the explicit instantiation of A<int> of A.cpp in the two files. Do I
    need to declare class A<int> in the two files? I am not sure how to do
    it?

    Any help are appreciated.


    Hua
     
    , Jan 1, 2008
    #1
    1. Advertising

  2. Barry Guest

    wrote:
    > I define a class in A.h:
    >
    > template <class T> class A{
    >
    > public:
    > void get_elem( const T&) ;
    > };
    >
    >
    >
    > Then I give the implementation in A.cpp (and do an explicit
    > instantiation ):


    Separating a template class into hxx file and cxx file should be done
    with keyword 'export' support, which is rarely supported by compilers
    (IIRC, Comeau supports it).

    So when you define a template class, just put all the implementation in
    hxx files.

    >
    > template<class T> void A::get_elem( const T&)
    > {...}
    >
    > template class A<int>; //explicit instantiation
    >
    >
    >
    >
    > I have another two files: main.cpp and B.cpp. I would like to reuse
    > the explicit instantiation of A<int> of A.cpp in the two files. Do I
    > need to declare class A<int> in the two files? I am not sure how to do
    > it?
    >


    Don't worry about the duplicated instantiation, the compiler is smart
    enough to know which she's already instantiated.

    > Any help are appreciated.
    >


    I think you should find a textbook to start with template programming first.
     
    Barry, Jan 1, 2008
    #2
    1. Advertising

  3. Salt_Peter Guest

    On Jan 1, 1:35 am, wrote:
    > I define a class in A.h:
    >
    > template <class T> class A{
    >
    > public:
    > void get_elem( const T&) ;
    >
    > };
    >
    > Then I give the implementation in A.cpp (and do an explicit
    > instantiation ):
    >
    > template<class T> void A::get_elem( const T&)
    > {...}
    >
    > template class A<int>; //explicit instantiation
    >
    > I have another two files: main.cpp and B.cpp. I would like to reuse
    > the explicit instantiation of A<int> of A.cpp in the two files. Do I
    > need to declare class A<int> in the two files? I am not sure how to do
    > it?
    >
    > Any help are appreciated.
    >
    > Hua



    Have you consulted the FAQ?
    [35.15] How can I avoid linker errors with my template classes?
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.15
     
    Salt_Peter, Jan 1, 2008
    #3
  4. qqq Guest

    On Jan 1, 3:24 am, Barry <> wrote:
    > wrote:
    > > I define aclassin A.h:

    >
    > >template<classT>classA{

    >
    > > public:
    > >     void get_elem( const T&) ;
    > > };

    >
    > > Then I give the implementation in A.cpp (and do anexplicit
    > >instantiation):

    >
    > Separating atemplateclassinto hxx file and cxx file should be done
    > with keyword 'export' support, which is rarely supported by compilers
    > (IIRC, Comeau supports it).
    >
    > So when you define atemplateclass, just put all the implementation in
    > hxx files.
    >
    >
    >
    > >template<classT> void A::get_elem( const T&)
    > >  {...}

    >
    > >templateclassA<int>; //explicitinstantiation

    >
    > > I have another two files: main.cpp and B.cpp. I would like to reuse
    > > theexplicitinstantiationof A<int> of A.cpp in the two files. Do I
    > > need to declareclassA<int> in the two files? I am not sure how to do
    > > it?

    >
    > Don't worry about the duplicatedinstantiation, the compiler is smart
    > enough to know which she's already instantiated.
    >
    > > Any help are appreciated.

    >
    > I think you should find a textbook to start withtemplateprogramming first.


    Thanks for your reply. I have read many c++ books, but no one talk
    the explicit instantiation very well.

    I prefer to the explicit instantiation instead of inline. But when I
    do an explicit instantiation in one .cpp file, I got some link error
    when more than one files are needing to use the explicit instantiation
    (if just one file, it can work.).
     
    qqq, Jan 1, 2008
    #4
  5. Barry Guest

    qqq wrote:
    > On Jan 1, 3:24 am, Barry <> wrote:
    >> wrote:
    >>> I define aclassin A.h:
    >>> template<classT>classA{
    >>> public:
    >>> void get_elem( const T&) ;
    >>> };
    >>> Then I give the implementation in A.cpp (and do anexplicit
    >>> instantiation):

    >> Separating atemplateclassinto hxx file and cxx file should be done
    >> with keyword 'export' support, which is rarely supported by compilers
    >> (IIRC, Comeau supports it).
    >>
    >> So when you define atemplateclass, just put all the implementation in
    >> hxx files.
    >>
    >>
    >>
    >>> template<classT> void A::get_elem( const T&)
    >>> {...}
    >>> templateclassA<int>; //explicitinstantiation
    >>> I have another two files: main.cpp and B.cpp. I would like to reuse
    >>> theexplicitinstantiationof A<int> of A.cpp in the two files. Do I
    >>> need to declareclassA<int> in the two files? I am not sure how to do
    >>> it?

    >> Don't worry about the duplicatedinstantiation, the compiler is smart
    >> enough to know which she's already instantiated.
    >>
    >>> Any help are appreciated.

    >> I think you should find a textbook to start withtemplateprogramming first.

    >
    > Thanks for your reply. I have read many c++ books, but no one talk
    > the explicit instantiation very well.
    >
    > I prefer to the explicit instantiation instead of inline. But when I
    > do an explicit instantiation in one .cpp file, I got some link error
    > when more than one files are needing to use the explicit instantiation
    > (if just one file, it can work.).


    I didn't mean reading a textbook about "template explicit
    instantiation", which is quite a rarely(at least for me) used language
    feature.

    http://www.codeproject.com/KB/cpp/templatesourceorg.aspx
     
    Barry, Jan 2, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,559
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    661
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    5
    Views:
    515
  4. Replies:
    1
    Views:
    617
    Salt_Peter
    Dec 25, 2006
  5. Noah Roberts
    Replies:
    6
    Views:
    1,237
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page