如何处理c++内联模板上下文中的警告

How to handle with warning in C++ inline + template context?

本文关键字:上下文 警告 何处理 处理 c++      更新时间:2023-10-16

我面临一个有趣的问题:我在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:program microsoft visual studio10.0 vc atlmfc afxtempl.h包括2066

它肯定来自上面提到的"RemoveKey"行:如果我只是简单地注释掉那行,我就不会得到这个警告。

我知道,主要问题是,CTypedPtrMap对象使用const long作为密钥类型,但CMapWordToPtr将使用WORD(无符号短)代替它。但事实是:我需要const long作为键类型,因为我在这个映射中处理大约100万个数据条目,所以使用unsigned short类将无法进一步完成它的工作。

我试图将"RemoveKey"行或stdafx.h包含到以下表达式中,但都不起作用:

#pragma warning (disable: 4244)
// expression
#pragma warning (default: 4244)

请分享我关于这个问题的任何想法,我怎么能在不改变容器的oElementMap定义和行为的情况下解决这个警告,并且没有在项目设置中全局抑制/禁用这个警告,以及不改变VS2010提供的afxtempl.h文件。

Thanks for help:

安德鲁

我已经将它的定义替换为:CMap<long, long&, T*, T*&> oElementMap;。我不确定它是旧映射定义的"长对应",因此我做了几个测试来比较它们。

最后的解决方案是: