Solved

function template conditional execution of code

Posted on 2002-05-01
6
283 Views
Last Modified: 2010-08-05
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.AllocSysString();
#else
          MyVar.vt=VT_I2;
          MyVar.intVal=InVal;
#end if
          SafeArrayPutElement(insa,&ElemNo,&MyVar);
     };

------------------------------------------------------------------------------------

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...
0
Comment
Question by:smegghead
6 Comments
 
LVL 3

Expert Comment

by:Crius
ID: 6983911
Eee, well, bad to use precompiled directives like this in code. Probably wouldn't work well anyway.

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,&ElemNo,&MyVar);
    };

It also allocates and deallocates memory (And calls the VariantInit) functions automatically. If you allocate a BSTR, it must be deallocated somewhere afterall. :)
0
 
LVL 3

Expert Comment

by:Crius
ID: 6983913
Oops, sorry,

COleVariant MyVar(InVal);

Don't use the type of the variable, just the value. Hehe.
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 6984072
Here's one method to test for CString.

 template <class T>
bool IsCString(T)
 {
      return false;
 }

bool IsCString(CString)
 {
      return true;
 }

 template <class T>
void FuncXyz(T Src)
{
      if (IsCString(Src))
      {
           AfxMessageBox("Is CString");
      }
      else
      {
           AfxMessageBox("Not CString");
      }
}
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:ct.smith
ID: 6984086
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,&MyVar);
    };

template <CString> void PutToSA(SAFEARRAY* &insa,long ElemNo,CString& InVal)
    {
         VARIANT MyVar;
         VariantInit(&MyVar);
         MyVar.vt=VT_BSTR;
         MyVar.bstrVal=InVal.AllocSysString();
         SafeArrayPutElement(insa,&ElemNo,&MyVar);
    };

Small catch, I don't have VC6 handy to double check my example code, but the spirit of the solution is fairly straightforward.
0
 
LVL 1

Expert Comment

by:andymurd
ID: 6990398
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.AllocSysString();
        SafeArrayPutElement(insa,&ElemNo,&MyVar);
   };

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,&MyVar);
   };

NOTE: The CString version comes BEFORE the template so the compiler will pick that one first if appropriate.
0
 
LVL 10

Author Comment

by:smegghead
ID: 6991840
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now