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

Posted 21 June 2011 - 06:51 AM

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 TT in the same shared library "a" and an object of C<TT> is constructed in this library as well. One method ( ex: void C::f() ) is not specialized explicitly for type TT 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(){} in library "a".
Library "b" is not always linked to "a". If it's not linked then the default C<T>::f() is called (which does nothing).
If "b" is linked to "a" then explicitly specialized 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.

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

Posted 21 June 2011 - 03:04 PM

Well first off lets simplify this a bit so we can maybe ask a coherent question.

There is a template class Foo<T> declared in say "foo.hpp". And Inside of Foo there is a template function bar<TT>().

There are two .cpp files: FooBar.cpp and main.cpp -- FooBar.cpp declares a template specialization of say: Foo<int>::bar<double>() and the question is: if one uses Foo<int>::bar<double> in main.cpp will it call the default Foo<int>::bar<TT>() declared in foo.hpp OR will the linker resolve any conflict and bind to the specialization?

Its an interesting question and I *think* the answer is that the linker will not bind to specialization when in main.cpp because it is not declared where the compiler can see it when compiling main.cpp:

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 be ignored but I provide them to match my original post: "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 C<TT> is constructed in this library as well."

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.hpp ). 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.

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

Posted 22 June 2011 - 02:54 AM

Well I compiled your program in multiple compilers and while visual studio was willing to compile the example it used the default C::f function defined in foo.h

On the other hand MinGW (GCC 4.6.0) would not link it due to multiple definitions of C<int>::f()

I have tried many variations trying to get it to work and I have to say that I just don't think that it is possible. While template classes do have linkage I don't think it gets as granular as allowing explicit specialization across translation units. However you can find copies of the standard (draft copies are easier to find but may not represent the "truth" until it has been finalized).

I would give you my read on it but I am no standards lawyer and I get easily confused by the wording.

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

Posted 22 June 2011 - 05:51 AM

I tried to find something if the C++ standard before posting my question to classify my case as: standard conformed, or compiler/system specific/unpredictable. I didn't find that however I didn't find anything in the standard that says against the observed behavior either. Also I tried to find this case in g++ documentation and failed.

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

Posted 22 June 2011 - 08:09 AM

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.