c++ - 在 visual studio 2015 community c++ 中,如何修复警告 C4838 : conversion from 'unsigned int' to 'int' requires a narrowing conversion

标签 c++ visual-studio

我正在尝试在 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(0) 放在那里作为 0,但警告相同。

我怎样才能继续使用这个讨厌的编译器?

最佳答案

编译器警告的 unsigned intIDC_LISTNM_CLICK,因为显然 0 不是一个 unsigned int

所以这两个中的任何一个都应该被签名, ctrlevnt 应该是未签名的。

关于c++ - 在 visual studio 2015 community c++ 中,如何修复警告 C4838 : conversion from 'unsigned int' to 'int' requires a narrowing conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33963706/

相关文章:

c++ - 源代码舍入 float

用于套接字的 C++ 迭代器外观

c++ - 如何使用 Visual C++ 命令提示符构建 DLL?

visual-studio - 如何使用 tasks.json 构建和编译 typescript

c++ - QT用DOM读取XML文件

c++ - #ifdef _DEBUG 在主函数中

visual-studio - Visual Studio : How to open file from Windows command line to specific line in running instance of VS?

c++ - Microsoft C++ 优化编译器已停止工作

ios - 我正在尝试将 firebase 合并到我的 Unity 项目中,但不断收到有关 "Firebase.Crashlytics.Editor.dll"的错误

c++ - 位置未处理的异常:Microsoft C++ 异常:内存位置的 std::length_error