If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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(){}.

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.

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

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.