Question

[EDIT: further digging revealed a different root issue. I'm rephrasing the question, but leaving the old version below, for consistency with the answer by @Leo]

It seems VC++ (both under VS2005 & VS2010) allows me to dllexport classes with missing implementations! The following code builds fine:

// missingimp.h :
class __declspec(dllexport) MissingImp
{
  void DoStuff();   // no implementation anywhere
  void DoMoreStuff();  // neither for this
}

// missingimp.cpp
#include "missingimp.h"

The code both compiles and links fine (in dll configuration) - and of course statically linking with the resulting dll fails. Is this be a bug? is this behavior somehow by design??

[Old question:]

I'm trying to dllexport a class, that has a data member templated on a forward-declared type:

// Class2Export.h:

class ForwardDeclared ;

template<class T> class TemplatedClass
{
T *m_ptr;

public:
TemplatedClass() { m_ptr->DoSomething(); }

};

class __declspec(dllexport) ExportedClass
{
TemplatedClass<ForwardDeclared> TemplateForward;
};

// Class2Export.cpp:

#include "Class2Export.h"

(This is not a contrived example - in the real code TemplatedClass is a smart pointer, but that seems irrelevant to the issue at hand.)

This code fails to compile, with -

error C2027: use of undefined type 'ForwardDeclared'

That still makes some kind of sense, based on a reply from MS:

If TemplatedClass has a constructor then a constructor will be automatically generated for ExportedClass. Since ExportedClass is exported the compiler tries to export the constructor but fails to generate code for it because ForwardDeclared is unknown.

But I suspect that is not the ultimate answer, as when I declare (without even implementing!) a ctor for ExportedClass:

...
class __declspec(dllexport) ExportedClass
{
ExportedClass();
TemplatedClass<ForwardDeclared> TemplateForward;
};
...

both compile and link succeed (with a due warning*). The issue over at MS-connect seems abandoned - perhaps anyone can shed some light over this strange behaviour?

Thanks!

*EDIT: the generated warning is C4251 : class 'TemplatedClass ' needs to have dll-interface to be used by clients of class 'ExportedClass'

Was it helpful?

Solution 2

Eventually I found an answer in MS forums.

I'm linking to it here, in case it's useful to anyone some day.

OTHER TIPS

Unless I've got the wrong end of the stick, the issue is about when the ExportedClass constructor code is generated.

If the ExportedClass constructor code is generated before ForwardDeclared is declared (not forward-declared but properly declared) then you'll get an error, since the ExportedClass constructor implicitly calls the TemplatedClass constructor, and that calls a method on ForwardDeclared which is undefined.

Your final example, the one which compiles and links with a warning, works because the ExportedClass constructor is never defined. (Presumably the warning is that ExportedClass::ExportedClass does not exist. Since nothing actually tries to use it it's just a warning and not an error.) You've avoided the issue there. As it is, that code is not useful as nothing can create ExportedClass (it has no constructor) but if you define the constructor somewhere then everything should work fine, provided ForwardDeclared is declared/defined before then.

If you change your final example to this you should get the errors back again: (All that's added are two braces to give the ExportedClass constructor an empty body)

class __declspec(dllexport) ExportedClass
{
    ExportedClass() { } // Error here
    TemplatedClass<ForwardDeclared> TemplateForward;
};

And that code is what you are implicitly doing if you don't have a constructor for ExportedClass at all. In these cases the constructor's code is being generated there and then in the header file. On the other hand, when the constructor is declared without a body in the header you are leaving the code to be defined and generated somewhere else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top