smegghead
asked on
function template conditional execution of code
Hi,
I want to do the following
-------------------------- ---------- ---------- ---------- ---------- ---------- --------
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
#if typeof(T) is CString
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS ysString() ;
#else
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
#end if
SafeArrayPutElement(insa,& ElemNo,&My Var);
};
-------------------------- ---------- ---------- ---------- ---------- ---------- --------
However, I just made up the "#if typeof" statement
Is there another way of doing this ???
I know I can create another template just for the CString class, but this seems to defeat the purpose of templates...
I want to do the following
--------------------------
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
#if typeof(T) is CString
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS
#else
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
#end if
SafeArrayPutElement(insa,&
};
--------------------------
However, I just made up the "#if typeof" statement
Is there another way of doing this ???
I know I can create another template just for the CString class, but this seems to defeat the purpose of templates...
Oops, sorry,
COleVariant MyVar(InVal);
Don't use the type of the variable, just the value. Hehe.
COleVariant MyVar(InVal);
Don't use the type of the variable, just the value. Hehe.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Seems to me like this is a good case for template specialisation:
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
SafeArrayPutElement(insa,& ElemNo,&My Var);
};
template <CString> void PutToSA(SAFEARRAY* &insa,long ElemNo,CString& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS ysString() ;
SafeArrayPutElement(insa,& ElemNo,&My Var);
};
Small catch, I don't have VC6 handy to double check my example code, but the spirit of the solution is fairly straightforward.
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
SafeArrayPutElement(insa,&
};
template <CString> void PutToSA(SAFEARRAY* &insa,long ElemNo,CString& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS
SafeArrayPutElement(insa,&
};
Small catch, I don't have VC6 handy to double check my example code, but the spirit of the solution is fairly straightforward.
ct.smith is correct, but I think an overload rather than template specialzation is more appropriate.
So you would have:
void PutToSA(SAFEARRAY* &insa,long ElemNo,CString& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS ysString() ;
SafeArrayPutElement(insa,& ElemNo,&My Var);
};
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
SafeArrayPutElement(insa,& ElemNo,&My Var);
};
NOTE: The CString version comes BEFORE the template so the compiler will pick that one first if appropriate.
So you would have:
void PutToSA(SAFEARRAY* &insa,long ElemNo,CString& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_BSTR;
MyVar.bstrVal=InVal.AllocS
SafeArrayPutElement(insa,&
};
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
VARIANT MyVar;
VariantInit(&MyVar);
MyVar.vt=VT_I2;
MyVar.intVal=InVal;
SafeArrayPutElement(insa,&
};
NOTE: The CString version comes BEFORE the template so the compiler will pick that one first if appropriate.
ASKER
Hi,
Thanks for your advice everyone, I have, in the interim, gone for the function overload solution, as this works fine.
However, I really wanted a way of branching code depending on the type/class passed in... it seems strange that the compiler doesn't have some way of doing this. It would make code a lot simpler and avoid a lot of duplicate code.
crius... I wasn't suggesting using pre-processor directives, this was just an example of the sort of thing I wanted to do.
I'm inclined to give the points to axter, as he/she came up with a solution I'd not thought of/tried.
So I'm gonna pass the points on to axter...
Sorry everyone else... I do appreciate your suggestions...
Regards
Smg.
Thanks for your advice everyone, I have, in the interim, gone for the function overload solution, as this works fine.
However, I really wanted a way of branching code depending on the type/class passed in... it seems strange that the compiler doesn't have some way of doing this. It would make code a lot simpler and avoid a lot of duplicate code.
crius... I wasn't suggesting using pre-processor directives, this was just an example of the sort of thing I wanted to do.
I'm inclined to give the points to axter, as he/she came up with a solution I'd not thought of/tried.
So I'm gonna pass the points on to axter...
Sorry everyone else... I do appreciate your suggestions...
Regards
Smg.
Try using COleVariant. It has a constructor that takes in all sorts of different variable types.
template <class T> void PutToSA(SAFEARRAY* &insa,long ElemNo,T& InVal)
{
COleVariant MyVar(T);
SafeArrayPutElement(insa,&
};
It also allocates and deallocates memory (And calls the VariantInit) functions automatically. If you allocate a BSTR, it must be deallocated somewhere afterall. :)