boost thread warning C4191 and algorithm warning C4242

Keywords: MySQL Windows

Today, I took time to sort out the warning information in the project. I found several warnings were magical. After some verification, I finally found the problem.

First warning:

boost/thread/win32/thread_primitives.hpp(315): warning C4191: "type conversion": unsafe conversion from "boost::detail::win32::farproc_t" to "boost::detail::win32::detail::gettickcount64_t"
1 > Calling the function through the result pointer may cause the program to fail

Reason: In addition to boost library, the third-party library in my project also refers to cryptopp565 library. Through the step-by-step tracing of the header file, I found that the header file config.h of cryptopp565 has the following definition.

#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

It is here that pragma warning once releases this warning from boost, which is normally blocked by 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;
            }

Solution: Change the order of header files, including boost/thread.hpp, config.h, or standard library threads, but it's strange that std:: threads are not from boost:: threads, it seems that they are not completely copied, at least there must be differences in the implementation methods. Take a look at the source code in another day.

The second warning:

1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1015): warning C4242: "=": From "int"Switch to "char",Possible loss of data
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1027): note: See Instantiation of the Function Template being compiled“_OutIt std::_Transform<char*,_OutIt,int(__cdecl *)(int)>(_InIt,_InIt,_OutIt,_Fn1)"Quotation
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: See Instantiation of the Function Template being compiled“_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)"Quotation
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>          ]

The reason: at first I thought it was the problem of sqlstring encapsulation in mysql connector, and later discovered that it was also the problem of pragma warning once above. It opened the 4242 alarm, but had to Tucao mysql connector c++ package was not so satisfactory (currently considering the interface for pure c).

Solution: Modify the order of header files

Posted by solarisuser on Wed, 13 Feb 2019 20:24:18 -0800