boost thread warning C4191和algorithm warning C4242

今天抽出时间来整理一下项目中的警告信息,发现有几个警告很神奇,经过一番查证,终于找到了问题所在。

第一个警告:

boost/thread/win32/thread_primitives.hpp(315): warning C4191: “类型转换”: 从“boost::detail::win32::farproc_t”到“boost::detail::win32::detail::gettickcount64_t”的不安全转换
1>          通过结果指针调用该函数可能导致程序失败

原因:因为我项目中第三方库除了boost库,还引用了cryptopp565这个库,通过头文件的逐级追查,发现在cryptopp565的头文件config.h中有如下定义


#ifdef _MSC_VER
    // 4127: conditional expression is constant
    // 4231: nonstandard extension used : 'extern' before template explicit instantiation
    // 4250: dominance
    // 4251: member needs to have dll-interface
    // 4275: base needs to have dll-interface
    // 4505: unreferenced local function
    // 4512: assignment operator not generated
    // 4660: explicitly instantiating a class that's already implicitly instantiated
    // 4661: no suitable definition provided for explicit template instantiation request
    // 4786: identifer was truncated in debug information
    // 4355: 'this' : used in base member initializer list
    // 4910: '__declspec(dllexport)' and 'extern' are incompatible on an explicit instantiation
#   pragma warning(disable: 4127 4231 4250 4251 4275 4505 4512 4660 4661 4786 4355 4910)
    // Security related, possible defects
    // http://blogs.msdn.com/b/vcblog/archive/2010/12/14/off-by-default-compiler-warnings-in-visual-c.aspx
#   pragma warning(once: 4191 4242 4263 4264 4266 4302 4826 4905 4906 4928)
#endif

正是这里的pragma warning once释放了boost的这一警告,一般情况下是被boost屏蔽掉的

 inline detail::gettickcount64_t GetTickCount64_()
            {
                static detail::gettickcount64_t gettickcount64impl;
                if(gettickcount64impl)
                    return gettickcount64impl;

                // GetTickCount and GetModuleHandle are not allowed in the Windows Runtime,
                // and kernel32 isn't used in Windows Phone.
#if BOOST_PLAT_WINDOWS_RUNTIME
                gettickcount64impl = &GetTickCount64;
#else
                farproc_t addr=GetProcAddress(
#if !defined(BOOST_NO_ANSI_APIS)
                    GetModuleHandleA("KERNEL32.DLL"),
#else
                    GetModuleHandleW(L"KERNEL32.DLL"),
#endif
                    "GetTickCount64");
                if(addr)
                    gettickcount64impl=(detail::gettickcount64_t) addr;
                else
                    gettickcount64impl=&GetTickCount64emulation;
#endif
                return gettickcount64impl;
            }

解决办法:改变头文件包含顺序,先include “boost/thread.hpp”;再include “config.h”;或者换标准库的thread,不过也奇怪了,std::thread不是从boost::thread搬来的吗,看样子并没有完全照搬,至少实现方法上肯定有差异,改天撸一遍源码看看

第二个警告:

1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1015): warning C4242: “=”: 从“int”转换到“char”,可能丢失数据
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1027): note: 参见对正在编译的函数 模板 实例化“_OutIt std::_Transform<char*,_OutIt,int(__cdecl *)(int)>(_InIt,_InIt,_OutIt,_Fn1)”的引用
1>          with
1>          [
1>              _OutIt=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>              _InIt=char *,
1>              _Fn1=int (__cdecl *)(int)
1>          ]
1>  c:\program files\mysql\connector.c++ 1.1\include\cppconn\sqlstring.h(111): note: 参见对正在编译的函数 模板 实例化“_OutIt std::transform<std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,int(__cdecl *)(int)>(_InIt,_InIt,_OutIt,_Fn1)”的引用
1>          with
1>          [
1>              _OutIt=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>              _InIt=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,
1>              _Fn1=int (__cdecl *)(int)
1>          ]

原因:起初我以为是mysql connector中 sqlstring封装的问题,后来发现也是上边pragma warning once的问题,它打开了4242报警,不过不得不吐槽mysql connector c++封装的不那么如人意(目前考虑换下纯c的接口)…

解决办法:修改头文件顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值