1

I am using Visual Studio right now. I have a classic macro to mark dll api functions and classes. My understanding is that marking a class dllexport makes it's functions visible from other binaries, making possible to use the type provided by the dll.

I would like to know what is the behaviour of dllexport and dllimport when the class contain template member functions? My initial suspicion is that it just ignore them, and that would be exactly what I want.

But I can't find where in the MSDN they explain this specific case (they explain other cases though).

Example:

    class MY_API Log // MY_API is either __declspec( dllexport ) or __declspec( dllimport ) depending on if we are compiling the library or using it's header
    {
    public:

            // is this ignored?
        template< typename TypeOfSomething >
        Log& write( const TypeOfSomething& something ) 
        {
            test_stream << something;
        }

        ~Log(); // ok this is exported/imported

    private:
          std::stringstream text_stream;
    };

I simplified the code to give this example.

What I suspect is that if the client code use the write() function, it will instantiate the template, generating a real function code, maybe inlined. In this case, will the compiler mark it "dllimport" as it should with all member functions, or will it do nothing and just use that version in the client code?

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • I understand that, what I'm looking for is for an explicit official answer to "will dllexport/import do anything with this member function or not?". – Klaim Jul 19 '12 at 14:45
  • I think you are in the "gray area". Most likely this will not work. – Kirill Kobelev Jul 19 '12 at 14:47
  • I'll add my suspiscions. – Klaim Jul 19 '12 at 14:48
  • @KirillKobelev What do you mean by "this will not work"? The problem is that it does compile and run apparently fine in my tests. But I don't know what is the official behaviour is supposed to be. – Klaim Jul 19 '12 at 14:51
  • This may be of help: http://support.microsoft.com/kb/168958 – Laserallan Jul 19 '12 at 15:04
  • @Laserallan No, this is about the template member function, not the member object. The link talk only about template/STL member objects. – Klaim Jul 19 '12 at 15:51
  • 1
    I have no explicit official answer for you but [an answer to a related question](http://stackoverflow.com/a/17527232/1532020) contains a link to [some further info](http://www.codesynthesis.com/~boris/blog/2010/01/18/dll-export-cxx-templates/). In short, yes, it seems that the dllexport is "ignored" for the function template. But this behavior is apparently specific to VS only. – vvnurmi Dec 31 '14 at 15:46

1 Answers1

1

There's no such thing as "template function". C++ has function templates.

Function templates are not functions. They are recipes to generate functions. They do not exist in a compiled program or DLL. They have no type and no address. You cannot import or export them.

Functions generated from templates (a.k.a. function template instantiations) are normal functions. They can be put in a DLL, imported and exported. But it usually makes little sense to do so. Function templates usually exist in the source form right in the header file, so there's no need import an instantiation from a DLL. Especially given that a DLL can only contain a fixed, finite set of instantiations, and the user of the library can always ask for an instantiation that isn't there.

Sometimes placing a specific fixed set of instantiations into a DLL is justified, but such cases are rare.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • 1
    I obviously know all that, that's not what I'm asking. I'm asking if using dllexport/import keyword have an impact or not, from official sources if possible, even if logically they shouldn't do anything. For example, even following the logic you describe, it could be possible for a member function to be imported if instanciated, as it would generate a real function in the client code. Such code would then be marked dllimport while it shouldn't. – Klaim Jul 19 '12 at 14:47
  • @Klaim, the function generated from a template should have the attributes that were given to it in the declaration. Any conflict should be treated the same as it would in non-template code - what happens if you declare a regular function `dllimport` but give it a body? – Mark Ransom Jul 19 '12 at 14:59
  • @MarkRansom There are three possibilities and they are antagonist, that's why I made this question. 1) the generated function is marked dllimport because the client code generated it. then a link error should occur as the function shouldn't be imported but just used. 2) the generated function is inlined, making dllimport ignored. 3) the compiler see that this is a function template and remove/ignore the dllimport/export command and do nothing additional on code generation. When testing: no linking error, and no apparent strange behaviour. This could be 2) or 3), but I don't know "The Rule". – Klaim Jul 19 '12 at 15:56
  • Your example code says nothing about explicit instantiations. Microsoft does not seem to explicitly specify the behaviour for this case anywhere. Experiments with VS suggest that explicit instantiations of member templates of exported classes are not automatically exported. – n. m. could be an AI Jul 19 '12 at 16:10