You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/storage-classes-cpp.md
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -37,13 +37,15 @@ translation.priority.ht:
37
37
- "zh-tw"
38
38
---
39
39
# Storage classes (C++)
40
-
A *storage class* in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects and. A given object can have only one storage class. Variables defined within a block have automatic storage unless otherwise specified using the `extern`, `static`, or `thread_local` specifiers. Automatic objects and variables have no linkage; they are not visible to code outside the block.
40
+
A *storage class* in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects. A given object can have only one storage class. Variables defined within a block have automatic storage unless otherwise specified using the `extern`, `static`, or `thread_local` specifiers. Automatic objects and variables have no linkage; they are not visible to code outside the block.
41
41
42
42
**Notes**
43
43
44
44
1. The [mutable](../cpp/mutable-data-members-cpp.md) keyword may be considered a storage class specifier. However, it is only available in the member list of a class definition.
45
45
46
46
2. Starting with [!INCLUDE[cpp_dev10_long](../build/includes/cpp_dev10_long_md.md)], the `auto` keyword is no longer a C++ storage-class specifier, and the `register` keyword is deprecated.
47
+
48
+
## In this section:
47
49
48
50
-[static](#static)
49
51
@@ -199,29 +201,31 @@ int main() {
199
201
A variable declared with the `thread_local` specifier is accessible only on the thread on which it is created. The variable is created when the thread is created, and destroyed when the thread is destroyed. Each thread has its own copy of the variable. On Windows, `thread_local` is functionally equivalent to the Microsoft-specific [__declspec( thread )](../cpp/thread.md) attribute.
200
202
201
203
```
202
-
thread_local float f = 42.0; //global namespace
204
+
thread_local float f = 42.0; // Global namespace. Not implicitly static.
203
205
204
206
struct C // cannot be applied to type definition
205
207
{
206
-
thread_local int i; //local
207
-
thread_local static char buf[10]; // local and static
208
+
thread_local int i; // Illegal. The member must be static.
209
+
thread_local static char buf[10]; // OK
208
210
};
209
211
210
212
void DoSomething()
211
213
{
212
-
thread_local C my_struct; // Apply thread_local to a variable
214
+
// Apply thread_local to a local variable.
215
+
// Implicitly “thread_local static C my_struct”.
216
+
thread_local C my_struct;
213
217
}
214
218
```
215
219
216
220
1. The thread_local specifier may be combined with `static` or `extern`.
217
221
218
-
2. You can apply `thread_local` only to data declarations and definitions; **thread_local** cannot be used on function declarations or definitions.
222
+
2. You can apply `thread_local` only to data declarations and definitions; `thread_local` cannot be used on function declarations or definitions.
219
223
220
-
3. The use of `thread_local` may interfere with [delay loading](../build/reference/linker-support-for-delay-loaded-dlls.md) of DLL imports**.**
224
+
3. The use of `thread_local` may interfere with [delay loading](../build/reference/linker-support-for-delay-loaded-dlls.md) of DLL imports.
221
225
222
226
4. On XP systems, `thread_local` may not function correctly if a DLL uses `thread_local` data and it is loaded dynamically via LoadLibrary.
223
227
224
-
5. You can specify `thread_local` only on data items with static storage duration. This includes global data objects (both **static** and `extern`), local static objects, and static data members of classes. You cannot declare automatic data objects with **thread_local**.
228
+
5. You can specify `thread_local` only on data items with static storage duration. This includes global data objects (both `static` and `extern`), local static objects, and static data members of classes. Any local variable declared `thread_local` will be implicitly static if no other storage class is provided; in other words, at block scope `thread_local` is equivalent to `thread_local static`.
225
229
226
230
6. You must specify `thread_local` for both the declaration and the definition of a thread local object, whether the declaration and definition occur in the same file or separate files.
0 commit comments