我正在尝试在 win32 API(无 mfc)上编写一些直接的 c++。 有了这个更现代的 c++ 编译器,我得到:
警告 C4838:从“unsigned int”到“int”的转换需要收缩转换
它发生在这段代码中:
EvMap<DlgCtl> DlgCtl::_evMap [] = {
{IDC_LIST, NM_CLICK, & DlgCtl::Pik},
{0}
};
riiiiight 在右大括号的前面。控件 ID 的 0 表示数组结束。 此代码将控件 ID 和事件 ID 映射到本地函数,类似于 MFC 的操作方式。
我想缩小转换意味着您在将 unsigned in 隐式转换为 int 时失去了精度(因为 unsigned 可以容纳 > 32767 而 int 不能)。
我不确定 unsigned int 是什么。有一个 0,但那是一个 signed int,对吧?
下面的代码使用列表来分派(dispatch) winproc-ish 类中的类局部函数。
template <class T>
struct EvMap {int ctrl, evnt; void (T::* func)(LPARAM l);};
template <class T>
bool DoEvMap (T *ths, EvMap<T> *evMap, int ctrl, int evnt, LPARAM l)
{
//DBG("DoEvMap ctrl=`d evnt=`d", ctrl, evnt);
for (int i = 0; evMap [i].ctrl; i++) {
//DBG(" - check ctrl=`d evnt=`d", evMap [i].ctrl, evMap [i].evnt);
if ((ctrl == evMap [i].ctrl) && (evnt == evMap [i].evnt))
{(ths->*(evMap [i].func)) (l); return true;}
}
return false;
}
我不想关闭警告,因为这是一种错误的形式。我尝试将 static_cast
我怎样才能继续使用这个讨厌的编译器?
最佳答案
编译器警告的 unsigned int
是 IDC_LIST
或 NM_CLICK
,因为显然 0
不是一个 unsigned int
。
所以这两个中的任何一个都应该被签名,或 ctrl
或 evnt
应该是未签名的。
关于c++ - 在 visual studio 2015 community c++ 中,如何修复警告 C4838 : conversion from 'unsigned int' to 'int' requires a narrowing conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33963706/