Thread: explicit specialization of methods of a template class in several .so

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    explicit specialization of methods of a template class in several .so

    Hi All,
    I need a confirmation that what I do is perfectly correct and I need to learn documents that confirm that.
    I have a template class (
    Code:
    template <class T> class C
    ) which is defined in the shared library "a". Some of the methods of the class C are specialized/defined explicitly for some particular type TT in the same shared library "a" and an object of
    Code:
    C<TT>
    is constructed in this library as well. One method ( ex: void
    Code:
    C::f()
    ) is not specialized explicitly for type TT in library "a" but specialized in another shared library "b". Just in case:
    Code:
    C<T>::f()
    has some compilable definition for any type T, ex:
    Code:
    template<class T> void C<T>::f(){}
    in library "a".
    Library "b" is not always linked to "a". If it's not linked the default
    Code:
    C<T>::f()
    is called (which does nothing).
    If "b" is linked to "a" then explicitly specialized
    Code:
    C<TT>::f()
    which resides in "b" is called. This is what I expect.
    Is it standard behavior? Is it documented somewhere? Where?
    I'm using gcc under linux and the code is compiled for FreeBSD.

    Thanks,
    Boris

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure what you mean when you say "Just in case: C<T>::f()has some compilable definition for any type T, ex: template<class T> void C<T>::f() in library "a"." If this is a .so file, then you don't have any compilable code at all, right? Unless you mean the compilable code is in some header that you can #include.

    (EDIT: Or perhaps you mean at the time a is compiled. In that case, when a is compiled, the compiler knows nothing about b, so C<TT>::f would only be created if it is instantiated in the library. If you don't instantiate it, but then do in library b, I wouldn't expect any clashes.)

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    tabstop, thank you for the reply. To clarify "compilable":
    Code:
    template<class T> void C<T>::f(){}
    is compilable(or rather compiler can generate a code whenever it needs) for any type T. This code is generated for type TT in library "a" and is called when library "b" is not linked "a".
    I don't have any clashes but I can't rely just on that. I need some a confirmation(preferably a reference to some documents) that this is a proper behavior and that it does not depend on the sequence of the loading of the libraries, etc.
    Last edited by tlknv; 06-21-2011 at 09:16 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    I got the answer at codeguru forum:
    C++0x-draft-n3092, 14.7.3.6
    If a template, a member template or the member of a class template is explicitly specialized then that
    specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required. If the program does not provide a definition for an explicit specialization and either the specialization is used in a way that would cause an implicit instantiation to take place or the member is a virtual member function, the program is ill-formed, no diagnostic required. An implicit instantiation is never generated for an explicit specialization that is declared but not defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-21-2011, 08:39 AM
  2. Replies: 3
    Last Post: 01-30-2011, 04:28 PM
  3. Replies: 6
    Last Post: 08-12-2007, 01:02 PM
  4. Partial specialization of class template.
    By CornedBee in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2003, 09:24 AM
  5. Template specialization
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2002, 07:08 AM

Tags for this Thread