Skip to content

Commit e775744

Browse files
authored
Merge pull request #278 from Microsoft/cr-hash-seq
Add _Hash_seq topic
2 parents 0fa9356 + 3737d56 commit e775744

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

docs/porting/TOC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
## [Upgrading Projects from Earlier Versions of Visual C++](upgrading-projects-from-earlier-versions-of-visual-cpp.md)
1212
### [Upgrade your code to the Universal CRT](upgrade-your-code-to-the-universal-crt.md)
1313
### [Modifying WINVER and _WIN32_WINNT](modifying-winver-and-win32-winnt.md)
14+
### [Fix your dependencies on library internals](fix-your-dependencies-on-library-internals.md)
1415
### [Floating-point migration issues](floating-point-migration-issues.md)
1516
### [Use native multi-targeting in Visual Studio to build old projects](use-native-multi-targeting.md)
1617
## [Introduction to Visual C++ for UNIX Users](introduction-to-visual-cpp-for-unix-users.md)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: "Fix your dependencies on library internals | Microsoft Docs"
3+
ms.custom: ""
4+
ms.date: "05/24/2017"
5+
ms.reviewer: ""
6+
ms.suite: ""
7+
ms.technology:
8+
- "cpp-language"
9+
ms.tgt_pltfrm: ""
10+
ms.topic: "article"
11+
dev_langs:
12+
- "C++"
13+
helpviewer_keywords:
14+
- "library internals in an upgraded Visual C++ project"
15+
- "_Hash_seq in an upgraded Visual C++ project"
16+
ms.assetid: 493e0452-6ecb-4edc-ae20-b6fce2d7d3c5
17+
caps.latest.revision: 1
18+
author: "corob-msft"
19+
ms.author: "corob"
20+
manager: "ghogen"
21+
translation.priority.ht:
22+
- "cs-cz"
23+
- "de-de"
24+
- "es-es"
25+
- "fr-fr"
26+
- "it-it"
27+
- "ja-jp"
28+
- "ko-kr"
29+
- "pl-pl"
30+
- "pt-br"
31+
- "ru-ru"
32+
- "tr-tr"
33+
- "zh-cn"
34+
- "zh-tw"
35+
---
36+
# Fix your dependencies on library internals
37+
38+
Microsoft has published the source code for the Standard Library, most of the C Runtime Library, and other Microsoft libraries in many versions of Visual Studio. The intent is to help you understand library behavior and to debug your code. One side-effect of publishing the library source code is that some internal values, data structures, and functions are exposed, even though they are not part of the library interface. They usually have names that begin with two underscores, or an underscore followed by a capital letter, names that the C++ Standard reserves to implementations. These values, structures, and functions are implementation details that may change as the libraries evolve over time, and so we strongly recommend against taking any dependencies on them. If you do, you risk non-portable code and issues when you try to migrate your code to new versions of the libraries.
39+
40+
In most cases, the What's New or Breaking Changes document for each release of Visual Studio doesn't mention changes to library internals. After all, you're not supposed to be affected by these implementation details. However, sometimes the temptation to use some code you can see inside the library is too great. This topic discusses dependencies on CRT or Standard Library internals you may have relied on, and how to update your code to remove those dependencies so you can make it more portable or migrate to new versions of the library.
41+
42+
## _Hash_seq
43+
44+
The internal hash function `std::_Hash_seq(const unsigned char *, size_t)`, used to implement `std::hash` on some string types, was visible in recent versions of the Standard Library. This function implemented an [FNV-1a hash]( https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) on a character sequence.
45+
46+
To remove this dependency, you have a couple of options.
47+
48+
- If your intent is to put a `const char *` sequence into an unordered container by using the same hash code machinery as `basic_string`, you can do that by using the `std::hash` template overload that takes a `std::string_view`, which returns that hash code in a portable way. The string library code may or may not rely on use of an FNV-1a hash in the future, so this is the best way to avoid a dependency on a particular hash algorithm.
49+
50+
- If your intent is to generate an FNV-1a hash over arbitrary memory, we've made that code available on GitHub in the [VCSamples]( https://github.com/Microsoft/vcsamples) repository in a stand-alone header file, [fnv1a.hpp](https://github.com/Microsoft/VCSamples/tree/master/VC2015Samples/_Hash_seq), under an [MIT license](https://github.com/Microsoft/VCSamples/blob/master/license.txt). We've also included a copy here for your convenience. You can copy this code into a header file, add the header to any affected code, and then find and replace `_Hash_seq` by `fnv1a_hash_bytes`. You'll get identical behavior to the internal implementation in `_Hash_seq`.
51+
52+
```cpp
53+
/*
54+
VCSamples
55+
Copyright (c) Microsoft Corporation
56+
All rights reserved.
57+
MIT License
58+
Permission is hereby granted, free of charge, to any person obtaining a copy of
59+
this software and associated documentation files (the "Software"), to deal in
60+
the Software without restriction, including without limitation the rights to
61+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
62+
of the Software, and to permit persons to whom the Software is furnished to do
63+
so, subject to the following conditions:
64+
The above copyright notice and this permission notice shall be included in all
65+
copies or substantial portions of the Software.
66+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
72+
SOFTWARE.
73+
*/
74+
#include <stddef.h>
75+
76+
inline size_t fnv1a_hash_bytes(const unsigned char * first, size_t count) {
77+
#if defined(_WIN64)
78+
static_assert(sizeof(size_t) == 8, "This code is for 64-bit size_t.");
79+
const size_t fnv_offset_basis = 14695981039346656037ULL;
80+
const size_t fnv_prime = 1099511628211ULL;
81+
82+
#else /* defined(_WIN64) */
83+
static_assert(sizeof(size_t) == 4, "This code is for 32-bit size_t.");
84+
const size_t fnv_offset_basis = 2166136261U;
85+
const size_t fnv_prime = 16777619U;
86+
#endif /* defined(_WIN64) */
87+
88+
size_t result = fnv_offset_basis;
89+
for (size_t next = 0; next < count; ++next)
90+
{ // fold in another byte
91+
result ^= (size_t)first[next];
92+
result *= fnv_prime;
93+
}
94+
return (result);
95+
}
96+
```
97+
98+
## See also
99+
100+
[Upgrading Projects from Earlier Versions of Visual C++](upgrading-projects-from-earlier-versions-of-visual-cpp.md)
101+
[Overview of potential upgrade issues (Visual C++)](overview-of-potential-upgrade-issues-visual-cpp.md)
102+
[Upgrade your code to the Universal CRT](upgrade-your-code-to-the-universal-crt.md)

0 commit comments

Comments
 (0)