如何处理c++内联模板上下文中的警告
How to handle with warning in C++ inline + template context?
我面临一个有趣的问题:我在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;
。我不确定它是旧映射定义的"长对应",因此我做了几个测试来比较它们。
最后的解决方案是:
- 警告处理为错误这里有什么问题
- #为""定义宏;静态";针对不同的上下文
- 与互斥锁相比,旋转锁可以保证上下文切换
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 使用typeid警告未使用的变量
- 示例C++项目编译中的警告
- 错误"Could not find Boost"(缺少:上下文标头)
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- OpenGL 调试上下文警告 -"将使用视频内存作为缓冲区异议的来源