我面临一个有趣的问题:我在 Visual C++ 6.0 中有一个 MFC 应用程序项目。由于 MFC 或 C++ 标准中有很多变化,我想将我的应用程序移植到 Visual Studio 2010。这很好,但我现在面临一个警告,我无法处理。
头文件具有以下类定义:
template <class T>
class foo : public CObject
{
// ...
// other stuff
// ...
private:
CTypedPtrMap<CMapWordToPtr, const long, T*> oElementMap;
void some_stuff();
}
在源文件中有:
template <class T>
void foo::some_stuff()
{
// ...
// other stuff
// ...
int nIndex = 0;
// ...
// other stuff
// ...
oElementMap.RemoveKey(nIndex);
}
当我尝试编译它时,我收到以下警告:
警告 1 警告 C4244:'argument':从 'const long' 转换为 'WORD',可能丢失数据 c:\programme\microsoft visual studio 10.0\vc\atlmfc\include\afxtempl.h 2066
它绝对来自上面提到的“RemoveKey”行:如果我只是简单地注释掉该行,我将不会收到此警告。
我知道,主要问题是,该CTypedPtrMap
对象const long
用作键类型,但CMapWordToPtr
会用WORD
(unsigned short) 代替它。但事实是:我需要 const long 作为键类型,因为我在这个映射中经常处理大约 100 万个数据条目,所以unsigned short
这个类将无法进一步完成它的工作。
我尝试将“RemoveKey”行或包含的嵌套stdafx.h
到以下表达式中,但都不起作用:
#pragma warning (disable: 4244)
// expression
#pragma warning (default: 4244)
请与我分享有关此问题的任何想法,我如何在不更改容器的oElementMap
定义和行为、不在项目设置中全局抑制/禁用此警告以及不更改afxtempl.h
VS2010 提供的文件的情况下解决此警告。
感谢帮助:
安德鲁