Re: dllexport vs. template + inheritance
"Imre" <imre42@pager.hu> wrote in message
news:1154863130.447804.146380@m79g2000cwm.googlegroups.com...
Hi
I have some problems with dll-exporting classes that are
derived from
template instances. It seems that if a class derived from
a template
class instance is exported, then the compiler wants to
explicitly
instantiate the whole template (including all member
functions), while
if the derived class is not exported, then only the class
declaration
is instantiated. I don't understand why, and it causes me
problems.
Some more detailed explanation follows, then my questions,
and some
sample code at the end.
Let's suppose I'm writing a library that is built as a
dll.
In the lib, there's a simple class template, called
DllTemplate. This
template is meant to be used with explicit instantiation
only, so it's
member functions are implemented in a separate header.
Most modules of
the code (both dll and client code) only include the
interface header
(DllTemplate.h), the implementation header
(DllTemplateImpl.h) is only
included in those modules that do the explicit instancing.
Let's have a class called DllDerived, which is derived
from
DllTemplate<int>. DllDerived.h only includes
DllTemplate.h, and
declares DllDerived, while DllDerived.cpp includes
DllTemplateImpl.h,
and explicitly instantiates DllTemplate<int>.
Let's also declare a DllMoreDerived class, which is
derived from
DllDerived.
Everythig works fine so far.
Now let's try to export DllDerived and DllMoreDerived. The
problem is
that as soon as DllDerived gets __declspec(dllexport), the
compiler
starts to issue warnings while compiling
DllMoreDerived.cpp. It says
that no suitable definition was provided for an explicit
template
instantiation request. It seems that exporting a class
derived from a
template class instance causes an explicit instantiation
of the whole
template, with all member functions as well.
So, my questions are:
- Why does the compiler fully instantiate the template, if
the derived
class is exported, while it instantiates only the class
declaration if
it's not exported?
Because compiler cannot predict which methods of the
template will be called by client. So, it must to export
everything. Here's the quote from MSDN article on
__declspec(dllexport):
-------
If a class is marked __declspec(dllexport), any
specializations of class templates in the class hierarchy
are implicitly marked as __declspec(dllexport). This means
templates are explicitly instantiated and its members must
be defined.
-------
- Can I safely ignore these warnings? My simple tests
indicate that
yes, they can be ignored, but I'd like to see some more
educated
opinions.
If you're talking about C4661 warning, then I wouldn't
ignore it so quickly. It's level 1 warning, i.e. one
slightest step before an error.
- Is there a solution that avoids the warnings, but
doesn't require
DllMoreDerived.h to include DllTemplateImpl.h?
The problem is that at the time `DllMoreDerived.cpp' is
compiled the definition of `DllTemplate<int>::F()' is not
visible anywhere for compiler. Hence the warning. I don't
kno whether it was deliberate decision or caused by chance.
However, this is what we have.
You can add `extern' specifier to `DllTemplate<int>'
template specialization and put it inside `DllTemplate.h'
instead of `DllDerived.cpp'. Then it will warn about
nonstandard extension used. However, [perhaps] it will make
compilers' life easier by telling to it that
`DllTemplate<int>' has full definition elsewhere.
Also, instead of raw `__declspec(dllexport)' I'd use macro
that will switch between dllexport and dllimport for DLL and
client compilation, respectively.
HTH
Alex