-
Can I export this?
I'm trying to export this (unfinished) template class in a DLL and it isn't working. No complaints from the compiler / linker, it's not in the list in depends (I haven't tried to import it yet, which I'm going to do dynamically).
Code:
template <typename T> class EXPORTED CArray
{
public:
CArray( size_t uSize ) : m_uSize( uSize )
{
// TODO: Attempt to allocate m_pData. Awaiting Debug library (Assert!)
}
~CArray( void )
{
// TODO: Free m_pData
}
// Random access
const T &operator[]( size_t uIndex ) const
{
// TODO: Assert validity of uIndex
return m_pData[uIndex];
}
T &operator[]( size_t uIndex )
{
// TODO: As above
return m_pData[uIndex];
}
const size_t &Size( void ) const
{
return m_uSize;
}
private:
T *m_pData;
size_t m_uSize;
};
-
Are you using the Comeau compiler or the Intel 7.x series? Because export is only supported by these two. And on the case of the Intel ones, I reckon it's off by default.
-
Sorry for forgetting this: MSVC 7.1 (VS .NET 2003).
Is there any way around this short of a slight redesign?
-
I'm not versed in the template separation model. Others will certainly be able to help. However whatever little readings I did on the subject have taught me that if you can mimic export without the export keyword, you are really not using export. In other words, the separate compilation model is only possible with the export keyword. Also, that export is extremely hard to use correctly and is probably best judged on a case-by-case basis.
If no one here will be able to help you any more than I did (I highly doubt it :) I can imagine at least 4 people who for sure know a lot about export), I suggest you get a book I bought a few months ago and that does have some coverage on the export feature of C++, Herb Sutter's Exceptional C++ Style 40 New Engineering Puzzles.
-
You're trying to put a template class in a DLL? Is that even possible?
A template doesn't define a class, it defines a way for the compiler to generate classes. E.g. std::vector<int> is a separate class from std::vector<char> or std::vector<float>. A template defines an infinite number of classes, which have analogous functions but each version of the same function will be very different in machine code.
A given instance of the template is only created when you use it in your code, and so the template can't be compiled unless you actually create an object based on that template.
I mean, the machine code generated for a function of vector<int> isn't the same as the machine code generated for the analogous function of vector<char>. Maybe I'm misunderstanding what you're trying to do.
In general if you want to make a template library, you'll do what STL does which is put all the template code inside header files and just #include them. You can't compile the template code as a DLL then link it, unless you want to restrict the templates to only specific classes.
For example, you could make and export the CArray<int> class from a DLL, but that wouldn't help you at all if you wanted to use CArray<double> or CArray<float> or CArray<UserDefinedClass>. There's a reason template classes and functions are generated only as needed -- there's an infinite number of possible classes that a single template could create.
-
Even the template separation model cannot make templates in DLLs work. In static libs, yeah, but not dynamics. (The DLL loader would have to instantiate templates.)
-
OK guys thanks. Admitedly I've still got a lot to learn about templates (and C++ in general) and I'll definately get that book (got to do something with my student loan!).