CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    2

    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 (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 (ex:int) in the same shared library "a" and an object of C<int> is constructed in this library as well. One method ( ex: void C::f() ) is not specialized explicitly for type int in library "a" but specialized in another shared library "b". Just in case: C<T>::f() has some compilable definition for any type T, ex: template<class T> void C<T>::f(){}.

    The simplified code can look like this:
    Code:
    --------------------------
    foo.h:
    template <class T> class C {
    public:
      void f()
      {
      }
      int aaa(int bbb)
      {
    	return bbb + 88;
      }
    };
    --------------------------
    foobar_a.cpp
    #include "foo.h"
    template <> int C<int>::aaa(int bbb)
    {
      return -555 - bbb;
    }
    C<int> global_c;
    --------------------------
    foobar_b.cpp
    #include "foo.h"
    #include "stdlib.h"
    template <> void C<int>::f()
    {
      exit(3);
    }
    --------------------------
    main_a.cpp
    #include "foo.h"
    
    extern C<int> global_c;
    
    int main()
    {
      global_c.f();
      return 0;
    }
    --------------------------
    foobar_a.cpp belongs to some shared/dynamic library "a".
    foobar_b.cpp belongs to some shared/dynamic library "b".
    main_a.cpp is either belongs to shared library "a" or linked to "a" anyhow.

    In the sample above neither of functions aaa(int) is needed and can actually be ignored.

    Shared library "b" may be linked to shared library "a" or maybe not linked depending on some particular application.
    I see that for the applications which have no "b" linked to "a" the default/generic implementation of f() is called ( which is defined in foo.h ). However for the applications which link "b" (dynamically) to "a" the explicitly defined specialization C<int>::f() is called ( which is defined in foobar_b.cpp ). I think that this is a proper and expected behavior however I need some document that confirms/profs that, ex: c++ standard or g++ documentation or something else.

    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
    Join Date
    Oct 2008
    Posts
    1,456

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

    the standard says not much about the compilation model, including things like (dynamic) libraries. The only guaranteed things are the one definition rule and the (conceptual) definitions of translation unit and instantiation unit, the latter essentially being what the translation unit looks like when all templates are instantiated and all non-instantiated templates are removed.

    in theory, once you get the conceptual picture of the instantiation units your program produces you have to deal only with ordinary functions and apply the compiler supplyed ordinary compilation model to them.

    the problem is that taking care of the full set of rules governing what/where/when gets instantiated it's somewhat cumbersome, so, personally, I avoid mixing templates with library exports.

    speaking about your example, according to

    Quote Originally Posted by 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.
    therefore, the declaration of all specializations should be visible in, say, "main_a.cpp", that is, they should be included in "foo.h"; so, AFAIK I don't think that the behavior you observed is guaranteed, although note that no diagnostic is required in this case.

  3. #3
    Join Date
    Jun 2011
    Posts
    2

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

    Thanks a lot superbonzo!
    You found exactly what I was looking for. Sadly I should not rely on the lucky behavior that I observed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured