C++ Hash Deprecation Warning

谁说胖子不能爱 提交于 2019-12-30 11:18:08

问题


I am very new to C++ and programming in general and am currently working through Bjarne Stroustrup's Programming: Principles and Practices using C++. I'm consistently receiving the error below

Severity Code Description Project File Line Error C2338 is deprecated and will be REMOVED. Please use . You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning.

I understand that the header file std_lib_facilities.h using some sort of deprecated function, but is there a way to bypass this? It looks like it wants me to define "_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS" but I'm unsure of how to do that. Any help would be appreciated!!


回答1:


The warning isn't about "some function" - it's about the whole of stdext. And it's not just hand-wavy, to be discontinued eventually, deprecated: it doesn't ship with 2015.

During the early 00's work was afoot to revise the C++ standard; different compiler vendors, Microsoft included, put proposals before the committee along with prototypes. So they could be tested and evaluated, Microsoft placed implementations of their proposed extensions in stdext.

Eventually the committee chose what they were going to incorporate in that revision and released a Technical Report ("TR1"). Anticipating completion before the end of 2009, this was referred to as "C++0x", and compiler vendors began implementing these features in the tr1 namespace. Finally in 2011 the standard was finalized and we got "C++11" with all its bit and pieces back in std where they belong.

According to Microsoft's proposal, the container would be std::hash_map, but the C++ committee chose to use the term unordered_map. std::map is an ordered container, stdext::hash_map, despite the name, is not.

Microsoft's compiler has been the slowest at getting full C++11 support finished, and the standards committee has since finished a second variation (C++14) and is working on a third (C++17). Microsoft is just-about finishing C++11 in VS2015 and a big chunk of C++14, with a few significant exceptions that are apparently going to be a major problem for the VS compiler (esp constexpr and template variables).

  1. Visual Studio 2015 does not provide stdext - it's gone. This is not one of those "well, it may eventually go away" cases.

  2. stdext is specific to the Microsoft family of compilers, so writing code using stdext:: anything is not portable: http://ideone.com/x8GsKY

  3. The standardized version of the feature you're wanting is std::unordered_map, you should use that. It's essentially the same thing.

  4. There are unresolved bugs in stdext::hash_map.

If you really have to use stdext::hash_map, silence the warning by adding

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS

at the top of the stdafx.h I assume your project has, or in your header files before you #include <stdext/...>, or in the solution explorer:

  • Right click on your project's entry in solution explorer,
  • Select Properties,
  • Select Configuration: All Configurations,
  • Expand the C/C++ tree entry,
  • Select Preprocessor,
  • The "Preprocessor Definitions" will probably say <different options>
  • At the beginning of the "Preprocessor Definitions" entry add _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS=1; so it reads _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS=1;<different options>. (or whatever was there originally should follow the ;)



回答2:


You can put the define prior to your including of the header generating the warning:

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include <hash_map>

You can also add the symbol in Proprocessor Definitions of the project file.

The later looks prettier, but given your doing something against the suggestion of the tool makers, I'd go with the first method, so you don't forget that you might get burnt latter.




回答3:


It seems that you used old "std_lib_facilities.h" header (stroustrup.com/Programming/std_lib_facilities.h).

New version of this header, working flawless for the "hello,world"-program in MSVS 2015, is available at stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Found it out when had had the same problem studying PPP.



来源:https://stackoverflow.com/questions/30430789/c-hash-deprecation-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!