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
In C++11, attributes provide a standardized way to annotate C++ constructs (including but not limited to classes, functions, variables, and blocks) with additional information that may or may not be vendor-specific. A compiler can use this information to generate informational messages, or to apply special logic when compiling the attributed code. The compiler ignores any attributes that it does not recognize, which means that you cannot define your own custom attributes using this syntax. Attributes are enclosed by double square brackets:
15
15
16
16
```
17
17
[[deprecated]]
18
-
void Foo(int);
19
-
18
+
void Foo(int);
20
19
```
21
20
22
-
Attributes represent a standardized alternative to vendor-specific extensions such as #pragma directives, __declspec() (Visual C++), or \__attribute\_\_ (GNU). However, you will still need to use the vendor-specific constructs for most purposes. The standard currently specifies three attributes that a conforming compiler should recognize:
21
+
Attributes represent a standardized alternative to vendor-specific extensions such as #pragma directives, __declspec() (Visual C++), or \__attribute\_\_ (GNU). However, you will still need to use the vendor-specific constructs for most purposes. The standard currently specifies three attributes that a conforming compiler should recognize:
23
22
24
23
-`noreturn` -- specifies that a function never returns; in other words it always throws an exception. The compiler can adjust its compilation rules for [[noreturn]] entities.
25
24
26
-
-`carries_dependency`--specifies that the function propagates data dependency ordering with respect to thread synchronization. The attribute can be applied to one or more parameters, to specify that the passed-in argument carries a dependency into the function body. The attribute can be applied to the function itself, to specify that the return value carries a dependency out of the function. The compiler can use this information to generate more efficient code.
25
+
-`carries_dependency`--specifies that the function propagates data dependency ordering with respect to thread synchronization. The attribute can be applied to one or more parameters, to specify that the passed-in argument carries a dependency into the function body. The attribute can be applied to the function itself, to specify that the return value carries a dependency out of the function. The compiler can use this information to generate more efficient code.
27
26
28
-
-`deprecated` – specifies that a function is not intended to be used, and might not exist in future versions of a library interface. The compiler can use this to generate an informational message when client code attempts to call the function. Can be applied to declaration of a class, a typedef-name, a variable, a non-static data member, a function, a namespace, an enumeration, an enumerator, or a template specialization.
27
+
-`deprecated` – **Visual Studio 2015 and later:**specifies that a function is not intended to be used, and might not exist in future versions of a library interface. The compiler can use this to generate an informational message when client code attempts to call the function. Can be applied to declaration of a class, a typedef-name, a variable, a non-static data member, a function, a namespace, an enumeration, an enumerator, or a template specialization.
29
28
30
29
-`fallthrough` - **Visual Studio 2017 and later:**(available with [/std:c++latest](../build/reference/std-specify-language-standard-version.md)) The `[[fallthrough]]` attribute can be used in the context of [switch](switch-statement-cpp.md) statements as a hint to the compiler (or anyone reading the code) that the fallthrough behavior is intended. The Visual C++ compiler currently does not warn on fallthrough behavior, so this attribute has no effect compiler behavior.
31
30
@@ -48,12 +47,12 @@ void Foo(int);
48
47
49
48
The example raises these warnings:
50
49
51
-
- 26494 (Type Rule 5: Always initialize an object.)
50
+
- 26494 (Type Rule 5: Always initialize an object.)
52
51
53
-
- 26485 (Bounds Rule 3: No array to pointer decay.)
52
+
- 26485 (Bounds Rule 3: No array to pointer decay.)
54
53
55
-
- 26481 (Bounds Rule 1: Don't use pointer arithmetic. Use span instead.)
54
+
- 26481 (Bounds Rule 1: Don't use pointer arithmetic. Use span instead.)
56
55
57
-
The first two warnings fire when you compile this code with the CppCoreCheck code analysis tool installed and activated. But the third warning doesn't fire because of the attribute. You can suppress the entire bounds profile by writing [[gsl::suppress(bounds)]] without including a specific rule number. The C++ Core Guidelines are designed to help you write better and safer code. The suppress attribute makes it easy to turn off the warnings when they are not wanted.
56
+
The first two warnings fire when you compile this code with the CppCoreCheck code analysis tool installed and activated. But the third warning doesn't fire because of the attribute. You can suppress the entire bounds profile by writing [[gsl::suppress(bounds)]] without including a specific rule number. The C++ Core Guidelines are designed to help you write better and safer code. The suppress attribute makes it easy to turn off the warnings when they are not wanted.
58
57
59
58
The C++ standard allows compiler vendors to define their own attribute parameters (within a vendor-specific namespace), but compilers are required to recognize only those attributes defined in the standard. In Visual C++, you can use the [[deprecated]] attribute instead of using declspec (deprecated) and the attribute will be recognized by any conformant compiler. For all other declspec parameters such as dllimport and dllexport, there is as yet no standard attribute equivalent so you must continue to use declspec syntax. Attributes do not affect the type system, and they don’t change the meaning of a program. Compilers ignore attribute values they don't recognize.
The following code example declares the late-specified return type of template function `Plus()`. The `Plus` function processes its two operands with the `operator+` overload. Consequently, the interpretation of the plus operator (+) and the return type of the `Plus` function depends on the types of the function arguments.
126
126
127
-
```
127
+
```cpp
128
128
// decltype_1.cpp
129
129
// compile with: /EHsc
130
130
//
@@ -200,6 +200,30 @@ int main()
200
200
201
201
42
202
202
203
+
## Example
204
+
**Visual Studio 2017 and later:** The compiler parses decltype arguments when the templates are declared rather than instantiated. Consequently, if a non-dependent specialization is found in the decltype argument, it will not be deferred to instantiation-time and will be processed immediately and any resulting errors will be diagnosed at that time.
205
+
206
+
The following example shows such a compiler error that is raised at the point of declaration:
207
+
208
+
```cpp
209
+
#include <utility>
210
+
template <class T, class ReturnT, class... ArgsT> class IsCallable
211
+
{
212
+
public:
213
+
struct BadType {};
214
+
template <class U>
215
+
static decltype(std::declval<T>()(std::declval<ArgsT>()...)) Test(int); //C2064. Should be declval<U>
216
+
template <class U>
217
+
static BadType Test(...);
218
+
static constexpr bool value = std::is_convertible<decltype(Test<T>(0)), ReturnT>::value;
Copy file name to clipboardExpand all lines: docs/cpp/deprecated-cpp.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "deprecated (C++) | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/28/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -37,15 +37,17 @@ translation.priority.ht:
37
37
- "zh-tw"
38
38
---
39
39
# deprecated (C++)
40
-
(Microsoft specific) With the exceptions noted below, the **deprecated** declaration offers the same functionality as the [deprecated](../preprocessor/deprecated-c-cpp.md) pragma:
40
+
This topic is about the Microsoft-specific deprecated declspec declaration. For information about the C++14 `[[deprecated]]` attribute, and guidance on when to use that attribute vs. the Microsoft-specific declspec or pragma, see [C++ Standard Attributes](attributes2.md).
41
+
42
+
With the exceptions noted below, the **deprecated** declaration offers the same functionality as the [deprecated](../preprocessor/deprecated-c-cpp.md) pragma:
41
43
42
44
- The **deprecated** declaration lets you specify particular forms of function overloads as deprecated, whereas the pragma form applies to all overloaded forms of a function name.
43
45
44
46
- The **deprecated** declaration lets you specify a message that will display at compile time. The text of the message can be from a macro.
45
47
46
48
- Macros can only be marked as deprecated with the **deprecated** pragma.
47
49
48
-
If the compiler encounters the use of a deprecated identifier, a [C4996](../error-messages/compiler-warnings/compiler-warning-level-3-c4996.md) warning is thrown.
50
+
If the compiler encounters the use of a deprecated identifier or the standard [`[[deprecated]]`](attributes2.md) attribute, a [C4996](../error-messages/compiler-warnings/compiler-warning-level-3-c4996.md) warning is thrown.
49
51
50
52
## Example
51
53
The following sample shows how to mark functions as deprecated, and how to specify a message that will be displayed at compile time, when the deprecated function is used.
Copy file name to clipboardExpand all lines: docs/cpp/static-assert.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,15 +40,15 @@ translation.priority.ht:
40
40
- "zh-tw"
41
41
---
42
42
# static_assert
43
-
Tests a software assertion at compile time. If the specified constant expression is `false`, the compiler displays the specified message and the compilation fails with error C2338; otherwise, the declaration has no effect.
43
+
Tests a software assertion at compile time. If the specified constant expression is `false`, the compiler displays the specified message, if one is provided, and the compilation fails with error C2338; otherwise, the declaration has no effect.
|`string-literal`|An message that is displayed if the `constant-expression` parameter is zero. The message is a string of characters in the [base character set](../c-language/ascii-character-set.md) of the compiler; that is, not [multibyte or wide characters](../c-language/multibyte-and-wide-characters.md).|
60
60
61
61
## Remarks
62
-
The `constant-expression` parameter of a `static_assert` declaration represents a *software assertion*. A software assertion specifies a condition that you expect to be true at a particular point in your program. If the condition is true, the `static_assert` declaration has no effect. If the condition is false, the assertion fails, the compiler displays the message in `string-literal` parameter, and the compilation fails with an error.
62
+
The `constant-expression` parameter of a `static_assert` declaration represents a *software assertion*. A software assertion specifies a condition that you expect to be true at a particular point in your program. If the condition is true, the `static_assert` declaration has no effect. If the condition is false, the assertion fails, the compiler displays the message in `string-literal` parameter, and the compilation fails with an error. In Visual Studio 2017 and later, the string-literal parameter is optional.
63
63
64
64
The `static_assert` declaration tests a software assertion at compile time. In contrast, the [assert Macro, _assert, _wassert](../c-runtime-library/reference/assert-macro-assert-wassert.md) macro tests a software assertion at run time and incurs a run time cost in space or time. The `static_assert` declaration is especially useful for debugging templates because template arguments can be included in the `constant-expression` parameter.
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2440.md
+46-1Lines changed: 46 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C2440 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/28/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -251,3 +251,48 @@ int main()
251
251
This error can appear in ATL code that uses the SINK_ENTRY_INFO macro defined in <atlcom.h>.
252
252
253
253
```
254
+
255
+
## Example
256
+
### Copy-list-initialization
257
+
258
+
Visual Studio 2017 and later correctly raise compiler errors related to object creation using initializer lists that were not caught in Visual Studio 2015 and could lead to crashes or undefined runtime behavior. As per N4594 13.3.1.7p1, in copy-list-initialization, the compiler is required to consider an explicit constructor for overload resolution, but must raise an error if that overload is actually chosen.
259
+
The following two examples compile in Visual Studio 2015 but not in Visual Studio 2017.
260
+
261
+
```
262
+
struct A
263
+
{
264
+
explicit A(int) {}
265
+
A(double) {}
266
+
};
267
+
268
+
int main()
269
+
{
270
+
A a1 = { 1 }; // error C3445: copy-list-initialization of 'A' cannot use an explicit constructor
271
+
const A& a2 = { 1 }; // error C2440: 'initializing': cannot convert from 'int' to 'const A &'
272
+
273
+
}
274
+
```
275
+
276
+
To correct the error, use direct initialization:
277
+
278
+
```
279
+
A a1{ 1 };
280
+
const A& a2{ 1 };
281
+
```
282
+
283
+
## Example
284
+
### cv-qualifiers in class construction
285
+
286
+
In Visual Studio 2015, the compiler sometimes incorrectly ignores the cv-qualifier when generating a class object via a constructor call. This can potentially cause a crash or unexpected runtime behavior. The following example compiles in Visual Studio 2015 but raises a compiler error in Visual Studio 2017 and later:
287
+
288
+
```
289
+
struct S
290
+
{
291
+
S(int);
292
+
operator int();
293
+
};
294
+
295
+
int i = (const S)0; // error C2440
296
+
```
297
+
298
+
To correct the error, declare operator int() as const.
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-2/compiler-error-c2668.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C2668 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/28/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -153,4 +153,26 @@ int main() {
153
153
pow(9,9); // C2668
154
154
pow((double)9,9); // OK
155
155
}
156
+
```
157
+
158
+
## Example
159
+
This code succeeds in Visual Studio 2015 but fails in Visual Studio 2017 and later with C2668. In Visual Studio 2015, the compiler erroneously treated copy-list-initialization in the same way as regular copy-initialization; it considered only converting constructors for overload resolution.
160
+
161
+
```
162
+
C++
163
+
struct A {
164
+
explicit A(int) {}
165
+
};
166
+
167
+
struct B {
168
+
B(int) {}
169
+
};
170
+
171
+
void f(const A&) {}
172
+
void f(const B&) {}
173
+
174
+
int main()
175
+
{
176
+
f({ 1 }); // error C2668: 'f': ambiguous call to overloaded function
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-2/compiler-error-c2955.md
+14-2Lines changed: 14 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C2955 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/28/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -87,4 +87,16 @@ int main() {
87
87
GC^ g; // C2955
88
88
GC <int>^ g;
89
89
}
90
-
```
90
+
```
91
+
92
+
## Example
93
+
**Visual Studio 2017 and later:** The compiler correctly diagnoses missing template argument lists when the template appears in a template parameter list (for example as part of a default template argument or a non-type template parameter). The following code compiles in Visual Studio 2015 but produces an error in Visual Studio 2017.
94
+
95
+
```
96
+
template <class T> class ListNode;
97
+
template <class T> using ListNodeMember = ListNode<T> T::*;
98
+
template <class T, ListNodeMember M> class ListHead; // C2955: 'ListNodeMember': use of alias
99
+
// template requires template argument list
100
+
101
+
// correct: template <class T, ListNodeMember<T> M> class ListHead;
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-2/compiler-error-c3066.md
+25-1Lines changed: 25 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C3066 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "03/28/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -71,4 +71,28 @@ int main() {
71
71
a(&i, &c); // C3066
72
72
a(&i, (const char *) &c); // OK
73
73
}
74
+
```
75
+
76
+
## Copy-list-initialization
77
+
In Visual Studio 2015, the compiler erroneously treated copy-list-initialization in the same way as regular copy-initialization; it considered only converting constructors for overload resolution. In the following example, Visual Studio 2015 chooses MyInt(23) but Visual Studio 2017 correctly raises the error.
78
+
79
+
```
80
+
// From http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#1228
81
+
struct MyList {
82
+
explicit MyStore(int initialCapacity);
83
+
};
84
+
85
+
struct MyInt {
86
+
MyInt(int i);
87
+
};
88
+
89
+
struct Printer {
90
+
void operator()(MyStore const& s);
91
+
void operator()(MyInt const& i);
92
+
};
93
+
94
+
void f() {
95
+
Printer p;
96
+
p({ 23 }); // C3066: there are multiple ways that an object of this type can be called with these arguments
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-warnings/compiler-warning-level-3-c4996.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,9 @@ The compiler encountered a deprecated declaration.
40
40
41
41
This warning or error has several possible meanings.
42
42
43
-
`C4996` occurs when the compiler encounters a function or variable that is marked as [deprecated](../../cpp/deprecated-cpp.md). Several functions, member functions, template functions, and global variables in the libraries in Visual Studio are marked as deprecated. These functions may have a different preferred name, may be insecure or have a more secure variant, or may be obsolete. The error message may include a suggested replacement for the deprecated function or global variable. You can turn this warning off with the [warning](../../preprocessor/warning.md) pragma or the **/wd4996** command line option. You can also use preprocessor macros to turn off certain classes of deprecation warnings.
43
+
`C4996` occurs when the compiler encounters a function or variable that is marked as [deprecated](../../cpp/deprecated-cpp.md). Several functions, member functions, template functions, and global variables in the libraries in Visual Studio are marked as deprecated. These functions may have a different preferred name, may be insecure or have a more secure variant, or may be obsolete. The error message may include a suggested replacement for the deprecated function or global variable. You can turn this warning off with the [warning](../../preprocessor/warning.md) pragma or the **/wd4996** command line option. You can also use preprocessor macros to turn off certain classes of deprecation warnings.
44
+
45
+
This warning is also issued when you attempt to access a function, class member or typedef that has the C++14 `[[deprecated]]` attribute. For more information, see [C++ Standard Attributes](../../cpp/attributes2.md).
44
46
45
47
**The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name:** new_name**. See online help for details.**
0 commit comments