In the base.h:
template <typename T, typename T2> class Base
{
public:
virtual int Foo(T param1) = 0;
virtual int Bar(T param1, T2 param2) = 0;
};
In the derived1.h:
template <typename T, typename T2> class Derived1 : public Base<T, T2>
{
public:
virtual int Foo(T param1);
virtual int Bar(T param1, T2 param2);
};
In the derived2.h:
template <typename T, typename T2> class Derived2 : public Base<T, T2>
{
public:
virtual int Foo(T param1);
virtual int Bar(T param1, T2 param2);
};
In myfile.cpp:
extern "C" template<typename T, typename T2> Base<T, T2> *my_func(template<typename T, typename T2> Base<T, T2> *param)
{
if( <cond1> )
param<char, char> = new Derived1<char, char>();
if( <cond2> )
param<SQLWCHAR, SQLWCHAR> = new Derived2<SQLWCHAR, SQLWCHAR>();
return param<T, T1>;
}
Is this code will compile in both MSVC 2010 and gcc?
I already had trouble with switching compiler, so wanted to ask first before getting there...
Also: I don't want the function to be templatized. I want the template object to be passed in and returned from the function.
Thank you.
[EDIT]
I just came home and tried:
extern "C" __declspec(dllexport) template<typename T, typename T2> Base<T, T2> *my_func(Base<T, T2> *param)
and got this:
warning C4091: '__declspec(dllexport)' : ignored on left of 'int' when no variable is declared
error C2143: syntax error : missing ';' before ''template<''
How do I make it work? The function needs to be exported and therefore it should have "C" linkage.
The other way is to define the function as C++ and grab its symbol name from the dll/so, which is painful.
[/EDIT]
[EDIT2}
What I actually had in mind is to write something like this:
class CMainFrame
{
private:
template<typename T, typename T2> Base<T, T2> *m_pBase;
public:
CMainFrame();
~CMainFrame();
int CallFunc();
};
CMainFrame::CMainFrame() {}
CMainFrame::~CMainFrame()
{
delete m_pBase;
}
int CMainFrame::CallFunc()
{
template<typename T, typename T2> Base<T, T2> *base = NULL;
m_pBase<T, T2> = my_func( base );
}
[/EDIT2]