Visual Studio warning macros for C++17 deprecated features

In my previous article, C++17 removed and deprecated features, I presented a list of the most important features that were either removed or deprecated in C++17. When you’re using deprecated features, compilers warn you about that. For instance, the following is an error message that you get when using std::not1()/std::not2() in Visual Studio 2017.

error C4996: ‘std::not1’: warning STL4008: std::not1(), std::not2(), std::unary_negate, and std::binary_negate are deprecated in C++17. They are superseded by std::not_fn(). You can define _SILENCE_CXX17_NEGATORS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.

In this article, I will show how you can silence these warnings when using Visual Studio 2017.

The following table lists all the VC++ macros that warn about features deprecated in C++17 (most of which are removed in C++20). Keep in mind that:

  • use of deprecated features is reported as an error (C4996), not warning;
  • these errors are raised when compiling with /std:c++17 or /std:latest;
  • when using the macros below to suppress compilation errors, you must define them before any standard library header has been included.
Macro Feature
_SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING <ccomplex>, <cstdalign>, <cstdbool>, <ctgmath>
_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING std::wbuffer_convert, std::wstring_convert, <codecvt> header
_SILENCE_CXX17_IS_LITERAL_TYPE_DEPRECATION_WARNING std::is_literal_type, std::is_literal_type_v
_SILENCE_CXX17_NEGATORS_DEPRECATION_WARNING std::not1(), std::not2(), std::unary_negate, std::binary_negate
_SILENCE_CXX17_RESULT_OF_DEPRECATION_WARNING std::result_of, std::result_of_t
_SILENCE_CXX17_TEMPORARY_BUFFER_DEPRECATION_WARNING std::get_temporary_buffer(), std::return_temporary_buffer

The _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS macro can be used to silence all the C++17 deprecation errors. Should you want to fine-grain the error messages and only silence the use of some deprecated features but not all, use specific macros designated for that particular features.

Appart from this macros, there are several others, not C++17 related.

Macro Feature
_SILENCE_IDENTITY_STRUCT_DEPRECATION_WARNING std::identity struct (non-stantard)

Some of the removed features can be actually brought back with the use of several other macros, that are listed below. These macros must be defined to value 1 but before any standard header is included. In some cases, they will trigger a deprecation error (C4996) and need to be used in conjunction with one of the macros from the previous tables.

Macro Feature
_HAS_AUTO_PTR_ETC std::auto_ptr
_HAS_FUNCTION_ALLOCATOR_SUPPORT std::function allocator support
_HAS_OLD_IOSTREAMS_MEMBERS deprecated iostream members
_HAS_TR1_NAMESPACE std::tr1 namespace
_HAS_TR2_SYS_NAMESPACE std::tr2::sys namespace

These macros can be found in the file VC\Tools\MSVC\<version>\include\yvals.h.

Note: You can change the C++ language standard for a project from the project properties if you go to Configuration Properties > C/C++ > Language > C++ Language Standard.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.