Programmer
Programmer

Reputation: 8707

Visual C++ Compiler Options

I need to ensure that when an implicit conversion occurs, compiler should throw warnings / errors as like in below code:

int32_t y = 9;
uint32_t x = y;
y = -1;
x = y;

In gcc I can make use of -Wconversion -Wsign-conversion compiler flags together to report such an issue - is there a similar options for VC++ builds?

Upvotes: 0

Views: 730

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41067

See Compiler Warnings That Are Off by Default

  • C4287 (level 3) 'operator': unsigned/negative constant mismatch

  • C4365 (level 4) 'action': conversion from 'type_1' to 'type_2', signed/unsigned mismatch

  • C4388 (level 4) signed/unsigned mismatch

  • C4287 (level 3) 'operator': unsigned/negative constant mismatch

There are others related to truncation.

Generally you'd use these by turning on all the warnings with /Wall and then building up a list of suppressions based on your code since a lot of these are really noisy.

For example, in DirectX Tool Kit for DX11, I use /Wall and the following suppressions in my pch.h:

// VS 2013 related Off by default warnings
#pragma warning(disable : 4619 4616 4350 4351 4472 4640 5038)
// C4619/4616 #pragma warning warnings
// C4350 behavior change
// C4351 behavior change; warning removed in later versions
// C4472 'X' is a native enum: add an access specifier (private/public) to declare a WinRT enum
// C4640 construction of local static object is not thread-safe
// C5038 can't use strictly correct initialization order due to Dev12 initialization limitations

// Off by default warnings
#pragma warning(disable : 4061 4265 4365 4571 4623 4625 4626 4628 4668 4710 4711 4746 4774 4820 4987 5026 5027 5031 5032 5039)
// C4061 enumerator 'X' in switch of enum 'X' is not explicitly handled by a case label
// C4265 class has virtual functions, but destructor is not virtual
// C4365 signed/unsigned mismatch
// C4571 behavior change
// C4623 default constructor was implicitly defined as deleted
// C4625 copy constructor was implicitly defined as deleted
// C4626 assignment operator was implicitly defined as deleted
// C4628 digraphs not supported
// C4668 not defined as a preprocessor macro
// C4710 function not inlined
// C4711 selected for automatic inline expansion
// C4746 volatile access of '<expression>' is subject to /volatile:<iso|ms> setting
// C4774 format string expected in argument 3 is not a string literal
// C4820 padding added after data member
// C4987 nonstandard extension used
// C5026 move constructor was implicitly defined as deleted
// C5027 move assignment operator was implicitly defined as deleted
// C5031/5032 push/pop mismatches in windows headers
// C5039 pointer or reference to potentially throwing function passed to extern C function under - EHc

// Windows 8.1 SDK related Off by default warnings
#pragma warning(disable : 4471 4917 4986 5029)
// C4471 forward declaration of an unscoped enumeration must have an underlying type
// C4917 a GUID can only be associated with a class, interface or namespace
// C4986 exception specification does not match previous declaration
// C5029 nonstandard extension used

It does have a maintenance cost to keep up with the compiler changes over time, which is in part why I have the comments to remind me what each one is for.

Upvotes: 2

Related Questions