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: rules/S8216/cfamily/rule.adoc
+28-7Lines changed: 28 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Relying on features that are not part of the configured {cpp} standard leads to
8
8
* Uses of features removed in the configured {cpp} standard.
9
9
* Uses of C-only constructs that some {cpp} implementations accept as extensions.
10
10
11
-
Keeping code within the configured standard ensures consistency across compilers and prevents accidental lock-in to permissive extensions.
11
+
Keeping code within the configured standard ensures consistency across compilers and prevents accidental lock-in to permissive extensions. Check the list of related rules for focused rules that report specific compiler extension cases; S8216 covers remaining standard violations not addressed by these specialized rules.
12
12
13
13
== How to fix it
14
14
@@ -88,24 +88,34 @@ int values[3] = {};
88
88
values[1] = 5;
89
89
----
90
90
91
-
=== Removed library feature
91
+
=== std::bind1st and std::bind2nd
92
92
93
-
Some features are removed in newer standards. For example, ``++std::auto_ptr++`` was removed in {cpp}17; using it when the configured standard has removed it is noncompliant.
93
+
The ``++std::bind1st++`` and ``++std::bind2nd++`` function templates were deprecated in {cpp}11 and removed in {cpp}17. When the configured standard is {cpp}17 or later, using these functions is noncompliant. Under earlier standards, the same code may be compliant but is still deprecated.
94
94
95
95
==== Noncompliant code example
96
96
[source,cpp,diff-id=5,diff-type=noncompliant]
97
97
----
98
-
#include <memory>
98
+
#include <functional>
99
99
100
-
std::auto_ptr<int> p(new int(42)); // Noncompliant in C++17 and later
100
+
auto bound = std::bind1st(std::greater<int>(), 5); // Noncompliant in C++17 or later
101
+
auto result = bound(3); // Returns true (5 > 3)
101
102
----
102
103
103
104
==== Compliant solution
104
105
[source,cpp,diff-id=5,diff-type=compliant]
105
106
----
106
-
#include <memory>
107
+
#include <functional>
107
108
108
-
std::unique_ptr<int> p = std::make_unique<int>(42);
109
+
auto bound = std::bind(std::greater<int>(), 5, std::placeholders::_1);
110
+
auto result = bound(3); // Returns true (5 > 3)
111
+
----
112
+
113
+
Or use a lambda expression:
114
+
115
+
[source,cpp,diff-id=5,diff-type=compliant]
116
+
----
117
+
auto bound = [](int x) { return std::greater<int>()(5, x); };
118
+
auto result = bound(3); // Returns true (5 > 3)
109
119
----
110
120
111
121
=== Limitations
@@ -119,3 +129,14 @@ This version primarily reports extensions starting from {cpp}20 and later. For e
119
129
* S3715 - GNU extensions should not be used
120
130
* S8230 - MSVC-specific extensions should not be used
121
131
* S8231 - Non-standard attributes should not be used
132
+
* S3731 reports usages of ``++auto++`` as a storage class specifier, which was removed in {cpp}11
133
+
* S7129 reports assignments of string literals to mutable char pointers, which is allowed only before {cpp}11
134
+
* S6172 - Designated initializers should be used in their C++ compliant form
135
+
* S2754 - Declarations should not be empty
136
+
* S796 - Only escape sequences defined in the ISO C standard should be used
137
+
* S3689 - Declaration specifiers should not be redundant
138
+
* S2324 - Flexible array members should not be declared
139
+
* S4997 reports usages of ``++std::auto_ptr++``, which was removed in {cpp}17
140
+
* S2668 reports usages of the increment operator on a ``++bool++`` variable, which was removed in {cpp}17
141
+
* S3522 reports usages of the ``++register++`` storage class specifier, which was removed in {cpp}17
142
+
* S5020 - Facilities in <random> should be used instead of "srand", "rand" and "random_shuffle"
0 commit comments