Question

On migrating a VC++ 6.0 developed code to Visual studio 2008, I got the below warnings

warning C4244: '=' : conversion from 'long' to 'VARTYPE', possible loss of data

Code Snippet:

STDMETHODIMP CTextFileParser::FormatString(
     long lTargetType, BSTR szFormat, BSTR szInput, VARIANT *value)
{
   //blahblahblah
   CComVariant vErg(szSource.c_str()); // Definition of vErg
   vErg.vt = lTargetType; // <<<< C4244

warning C4244: 'argument' : conversion from 'long' to 'VARTYPE', possible loss of data

Code Snippet:

STDMETHODIMP CField::get_ContentWithType(long lType, VARIANT *pVal)
{
   HRESULT hRC;
   CComVariant oNewValue(m_content);           // Definition of oNewValue
   hRC = oNewValue.ChangeType(lType, NULL);    // <<<< C4244
   if (SUCCEEDED(hRC)) oNewValue.Detach(pVal);
   return hRC;
}

I'm unable to fix this warning?..

Was it helpful?

Solution

VARTYPE is 16 bits.

typedef unsigned short VARTYPE;

In order to kill the warning, you should use old style C cast or preferably C++ static_cast.

vErg.vt = static_cast<VARTYPE>( lTargetType );

If you can, a better, but more complex, solution would be to refactor your code, passing VARTYPE arguments instead of long ones.

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