Compiler warning in boost crc.hpp with Visual C++ (error C4244) - c++

I am using boost/crc.hpp version 1.49 for crc calculation in Visual Studio 2013.
When I instantiate a variable of type
boost::crc_32_type
I get a compiler error
Error 7 error C4244: 'return' : conversion from 'unsigned int' to 'unsigned char', possible loss of data C:\boost_1_49_0_0\boost\crc.hpp 578 1 MyProgram
I found this ticket from over 5 years ago:
https://svn.boost.org/trac/boost/ticket/3194
Does anyone have an idea how to solve it? Do I have to patch crc.hpp?
My warning level is 4.

I am simply surpressing the warning now.
#pragma warning(disable: 4244)
#include <boost/crc.hpp>
#pragma warning(default: 4244)

Related

How do I prevent the compiler from compiling obvious typos like if (somevalue = 0)? [duplicate]

Is it possible in Visual Studio to get a compiler error for an assignment in an if statement? How?
#include <iostream>
int main()
{
int a = 2;
if (a = 3) // Want a warning here
std::cout << "Avoid this!\n";
}
I know I can switch to Yoda conditions (if (3=a)), but I really don't want to.
I tried: setting the warning level to /Wall but I still don't get a warning that I could then treat as an error.
I am doing this in Visual Studio 2019 (16.11.19).:
The build output is
Rebuild started...
1>------ Rebuild All started: Project: Yoda2019, Configuration: Debug Win32 ------
1>Yoda2019.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): error C2220: the following warning is treated as an error
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(154,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(164,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(284,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(299,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(315,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(375,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>Done building project "Yoda2019.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
and Visual Studio 2022 (17.4.0)
The build output is
Rebuild started...
1>------ Rebuild All started: Project: YodaCheck, Configuration: Debug x64 ------
1>YodaCheck.cpp
1>YodaCheck.vcxproj -> B:\Projekte\C++\Dynamic Linking\YodaCheck\x64\Debug\YodaCheck.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Elapsed 00:01,379 ==========
so I see no warning which I could convert into an error.
You can turn any specific warning into an error, using the #pragma warning(error:nnnn) directive. In your case, the warning is:
warning C4706: assignment within conditional expression
So, adding the relevant #pragma directive to the code will generate a compiler error:
#include <iostream>
#pragma warning(error:4706)
int main()
{
int a = 2;
if (a = 3) // Want a warning here
std::cout << "Avoid this!\n";
}
This now gives:
error C4706: assignment within conditional expression
For a solution wide setting, change it here in the property pages:
Use /W4 /WX
/WX Treats all compiler warnings as errors. For a new project, it may be best to use /WX in all compilations; resolving all warnings ensures the fewest possible hard-to-find code defects.
https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
Make sure you make that setting in the relevant configurations. In such a simple demo, it's probably easiest to choose "all configurations" and "all platforms".

Resurrecting Visual C++ 9.0 application

I've been handed an ancient C++ application and asked to see if I can get it building and running again. I first tried building it using Visual Studio 2022 and ran into a lot of problems, so thought I would try building it in the environment it was used to.
The VCPROJ starts with this:
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="FsxApp"
ProjectGUID="{D0B8B3A6-3037-46F2-93D2-40318F591EEE}"
RootNamespace="FsxApp"
Keyword="MFCProj"
TargetFrameworkVersion="131072"
>
Looking up Visual C++ 9.0 in wikipedia, it says that version maps to "Visual Studio 2008 9.0" so I installed Visual Studio 2008. For some reason, Visual Studio can't find the files that are right in front of it so I had to add $(ProjectDir) to the "Additional Include Directories".
Now, VS complains because it can't find afxwin.h so I added the path of my modern MFC folder, and then it couldn't find winsdkver.h and winapifamily.h, and I found those in subfolders of a folder titled "Windows Kits" that I added to the list.
And now it's complaining because
#if (WINVER < 0x0501)
#error MFC does not support WINVER less than 0x0501. Please change the definition of WINVER in your project properties or precompiled header.
#endif
and sure enough, my stdafx.h says
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0500 // Windows 2000 or later. (Necessary to use GetTitleBarInfo())
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0500 // Windows 2000 or later.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0500 // Windows 2000 or later.
#endif
and if I change all of those to 0x0501, it is very unhappy:
winnt.h(7063) : warning C4163: '_InterlockedExchangeAdd8' : not available as an intrinsic function
winnt.h(7349) : warning C4163: '__cpuidex' : not available as an intrinsic function
atltrace.h(92) : error C2065: 'nullptr' : undeclared identifier
atlwinverapi.h(714) : error C2159: more than one storage class specified
atlwinverapi.h(714) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
atlwinverapi.h(714) : error C2440: 'initializing' : cannot convert from 'PFNLCMAPSTRINGEX' to 'volatile int'
atlwinverapi.h(714) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
atlwinverapi.h(714) : error C2440: 'initializing' : cannot convert from 'PFNLCMAPSTRINGEX' to 'int'
atlwinverapi.h(714) : error C2446: '==' : no conversion from 'PFNLCMAPSTRINGEX' to 'int'
atlwinverapi.h(714) : error C2040: '==' : 'int' differs in levels of indirection from 'PFNLCMAPSTRINGEX'
atlwinverapi.h(714) : error C2440: '=' : cannot convert from 'PFNLCMAPSTRINGEX' to 'int'
atlwinverapi.h(714) : error C2440: '=' : cannot convert from 'PFNLCMAPSTRINGEX' to 'volatile int'
atlwinverapi.h(714) : error C2440: '=' : cannot convert from 'PFNLCMAPSTRINGEX' to 'int'
atlwinverapi.h(714) : error C2446: '!=' : no conversion from 'PFNLCMAPSTRINGEX' to 'int'
atlwinverapi.h(714) : error C2040: '!=' : 'int' differs in levels of indirection from 'PFNLCMAPSTRINGEX'
atlwinverapi.h(716) : error C2100: illegal indirection
atlwinverapi.h(716) : error C2064: term does not evaluate to a function taking 9 arguments
atlcore.h(37) : fatal error C1083: Cannot open include file: 'type_traits': No such file or directory
which isn't surprising at all, when one grabs random include folders.
So then I wondered if I could find the correct version of MFC. According to wikipedia, the MFC in use during Visual Studio 2008 was mfc90.dll but if you google "download mfc90.dll" you get a bunch of questionable links, and those that look semi-possible just have the DLLS, not the H files.
Is finding an old version of MFC my best bet? Is there some other way to get an old application building?

Atlbase won't include properly in Unreal Engine 4

I'm using the full version of VS2013, and am trying to include atlbase into a class, along with sphelper, but I'm getting various types of errors.
I'm using a newly generated class, which will cleanly compile without these inclusions, and has pretty much nothing else inside of it.
The compiler is finding the libraries and seems to load them, but then I get around 20 errors that are all pretty much just like this (I omitted the rest, but they are all just like these ones)
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atlcore.h(630): warning C4191: 'reinterpret_cast' : unsafe conversion from 'FARPROC' to 'BOOL (__cdecl *)(DWORD)'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(271): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNCREATETRANSACTION'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(321): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNCOMMITTRANSACTION'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(427): error C2039: 'DeleteFile' : is not a member of '`global namespace''
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(448): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNMOVEFILETRANSACTED'
1> Calling this function through the result pointer may cause your program to fail
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(460): error C2039: 'MoveFile' : is not a member of '`global namespace''
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atltransactionmanager.h(487): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'PFNGETFILEATTRIBUTESTRANSACTED'
1>E:\Programs\Microsoft Visual Studio 12.0\VC\ATLMFC\INCLUDE\atlbase.h(5766): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'LSTATUS (__cdecl *)(HKEY,LPCWSTR,REGSAM,DWORD)'
1> Calling this function through the result pointer may cause your program to fail
1>C:\Program Files (x86)\Windows Kits\8.1\include\um\sphelper.h(1333): warning C4191: 'type cast' : unsafe conversion from 'FARPROC' to 'LPFN_RegLoadMUIStringW'
1> Calling this function through the result pointer may cause your program to fail
These errors present themselves only after atlbase.h and/or sphelper.h are included. Half of them from the first, the other half from the second.
They are included as follows (beneath my project and class header inclusion):
#include <stdio.h>
#include <Windows.h>
#include "AllowWindowsPlatformTypes.h"
#include <atlbase.h>
#include "sphelper.h"
#include "HideWindowsPlatformTypes.h"
I have them within this 'platform types' block because the atlbase and sphelper libraries throw an obscene amount of errors each otherwise, pertaining to arbitrary declarations or something.
I have not edited the library files in any way, and completely deleted all of the libraries and reinstalled them from scratch.
It may be due to an oversight or something on my part, but can anyone explain why the atl and sphelper libraries won't include properly?
Edit:
To clarify, the solution to the problem in which I "resolved" that led to this problem, I found on "https://answers.unrealengine.com/questions/27560/trouble-using-windows-includes-with-dword-int.html"
I posted my problem on a more specific site, and got an answer there.
User Jamie Dale posted the following on UE4 AnswerHub
#include "AllowWindowsPlatformTypes.h"
#pragma warning(push)
#pragma warning(disable: 4191) // warning C4191: 'type cast' : unsafe conversion
#pragma warning(disable: 4996) // error C4996: 'GetVersionEx': was declared deprecated
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// atltransactionmanager.h doesn't use the W equivalent functions, use this workaround
#ifndef DeleteFile
#define DeleteFile DeleteFileW
#endif
#ifndef MoveFile
#define MoveFile MoveFileW
#endif
#include <atlbase.h>
#undef DeleteFile
#undef MoveFile
#include <sphelper.h>
#pragma warning(pop)
#include "HideWindowsPlatformTypes.h"
This work around replaces the inclusions I used, and completely solved all of the problems I had. Full credit towards Jamie Dale on there.

Visual Studio + Boost error

I'm trying to build a solution using Boost in Visual Studio C++ Express 2010 and get the following error:
1>------ Build started: Project: MEDsLDAc, Configuration: Release Win32 ------
1> MedLDA.cpp
1>D:\_download\boost_1_53_0\boost/math/constants/calculate_constants.hpp(152): error C2988: unrecognizable template declaration/definition
1>D:\_download\boost_1_53_0\boost/math/constants/calculate_constants.hpp(152): error C2059: syntax error : 'constant'
1>D:\_download\boost_1_53_0\boost/math/constants/calculate_constants.hpp(153): error C2143: syntax error : missing ')' before '>'
1>D:\_download\boost_1_53_0\boost/math/constants/calculate_constants.hpp(184): error C2244: 'boost::math::constants::detail::constant_euler<T>::compute' : unable to match function definition to an existing declaration
1> definition
1> 'T boost::math::constants::detail::constant_euler<T>::compute(void)'
1> existing declarations
1> 'T boost::math::constants::detail::constant_euler<T>::compute(void)'
1>MedLDA.cpp(385): warning C4244: 'initializing' : conversion from 'double' to 'long', possible loss of data
1>MedLDA.cpp(426): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(105) : see declaration of 'strcpy'
1>MedLDA.cpp(485): warning C4244: 'initializing' : conversion from 'double' to 'long', possible loss of data
1>MedLDA.cpp(608): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(105) : see declaration of 'strcpy'
1>MedLDA.cpp(609): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(105) : see declaration of 'strcpy'
1>MedLDA.cpp(636): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(105) : see declaration of 'strcpy'
1>MedLDA.cpp(730): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>MedLDA.cpp(963): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>MedLDA.cpp(1150): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>MedLDA.cpp(1241): warning C4244: 'initializing' : conversion from 'double' to 'long', possible loss of data
1>MedLDA.cpp(1296): warning C4244: 'initializing' : conversion from 'double' to 'long', possible loss of data
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
How to fix it?
UPDATE: the software is MedLDAc available here:
http://www.ml-thu.net/~jun/medlda.shtml
The author says building works for him.
Since you haven't shown any code, we can only guess. My guess is that you #define some macro that messes-up Boost.Math code. Maybe something like #define M ......
UPDATE: It turns out that my guess was correct. Take a look at MedLDAc/MEDsLDAc/cokus.h file:
#define M (397) // a period parameter
The immediate workaround is to move #include <boost/math/special_functions/gamma.hpp> line in MedLDA.cpp to be the first include, after StdAfx.h, so that the above macro wouldn't affect the templates declared in gamma.hpp.
P.S. Defining one-letter macros is a really, really bad idea.

Asio Compiler errors (Win7 64 Bit + VC11)

a few days ago I upgraded from Win7 32Bit + Visual Studio 2008 to Win7 64Bit + Visual Studio 2012 (VC11). Now I have problems with compiling my application. I get the following errors:
1>...\asio-1.5.3\include\asio/detail/impl/win_thread.ipp(51): error C2664: 'QueueUserAPC': cannot convert parameter 1 from 'void (__cdecl *)(ULONG)' to 'PAPCFUNC'
1>...\asio-1.5.3\include\asio/detail/impl/win_iocp_io_service.ipp(119): error C2664: 'GetQueuedCompletionStatus': cannot convert parameter 3 from 'DWORD *' to 'PULONG_PTR'
1>...\asio-1.5.3\include\asio/detail/impl/win_iocp_io_service.ipp(349): error C2664: 'GetQueuedCompletionStatus': cannot convert parameter 3 from 'DWORD *' to 'PULONG_PTR'
And I get the same errors with the latest version of boost::asio (Boost 1.52)
What’s wrong here?
with WINVER set to 0x0501 it's working - Many Thanks to Hans Passant for his hint!