Topic: export, extern template specialization, instantiation, and Microsoft


Author: "Brad Daniels" <brad.daniels@missioncritical.com>
Date: 1998/01/15
Raw View
I have written yet another string class.  Originally, it could handle either
char or wchar_t, depending on how it was built, but we needed to mix UNICODE
and ASCII in a single program, so I decided to templatize it.  I pulled out
my standard draft 2, and saw all this cool stuff on the "export" keyword.
If I understand it correctly, you declare a template like:

export template<class C> class String {
public:
    String(wchar_t *s);
    String(char *s);
    ...
}

In your header, then in a source file, you'd have the implementations of the
ctors and other non-inlined functions visible, and explicitly instantiate
the templates using syntax like:

template String<char>;
template String<wchar_t>;

This approach is kind of neat, but Microsoft doesn't support "export".  I
actually kind of prefer their syntax, and wonder why something similar
wasn't adopted.  With Microsoft's compiler, I put:

extern template String<char>;
extern template String<wchar_t>;

in my header...  This approach is nice because now I've said only that these
two specializations use explicit instantiation, and could allow users to
define additional specializations that either are or are not explicitly
instantiated.  At this point, however, things get ugly.  I assumed that I
could use the same syntax as in the standard for defining the non-inlined
functions, since that would map nicely to the declaration vs.. definition
model for extern variables.  Unfortunately, I get two messages at that
point.  The first is:

warning C4660: template-class specialization 'String<char>' is already
instantiated

I'm guessing it is caused by the fact that I have the code:

template<> String<char>::String(wchar_t *s) { /* convert s to ASCII */ }
template<> String<wchar_t>::String(char *s) { /* convert s to UNICODE */ }

Does specialization cause instantiation?  I don't think it should...

The second problem I encounter is an error:

error C2949: 'String<char>' : explicit instantiation; cannot use 'auto' and
'extern' on the same template-class specialization

Now that one really annoys me...I have no clue how to instantiate the
functions I've declared to be extern.  I tried removing the explicit
instantiation under the assumption that the above specializations might
instantiate as well, but the linker tells me no.  Does anyone know where I
should go from here?

- Brad
--
----------------------------------------------------------------------------
Brad Daniels                  |  Was mich nicht umbringt macht mich hungrig.
Mission Critical Software     |   - Otto Nietzche
Standard disclaimers apply    |     (Friedrich's corpulent brother)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]