|
1 | | -FIXME: add a description |
2 | | - |
3 | | -// If you want to factorize the description uncomment the following line and create the file. |
4 | | -//include::../description.adoc[] |
| 1 | +Code should not rely on features beyond the configured {cpp} standard |
5 | 2 |
|
6 | 3 | == Why is this an issue? |
7 | 4 |
|
8 | | -FIXME: remove the unused optional headers (that are commented out) |
| 5 | +Relying on features that are not part of the configured {cpp} standard leads to non-portable builds and fragile code. Some toolchains accept such code as extensions or for backward compatibility, while others strictly reject it under the same standard setting. This rule flags: |
| 6 | + |
| 7 | +* Uses of features from future {cpp} standards (compared to the configured standard). |
| 8 | +* Uses of features removed in the configured {cpp} standard. |
| 9 | +* Uses of C-only constructs that some {cpp} implementations accept as extensions. |
9 | 10 |
|
10 | | -//=== What is the potential impact? |
| 11 | +Keeping code within the configured standard ensures consistency across compilers and prevents accidental lock-in to permissive extensions. |
11 | 12 |
|
12 | 13 | == How to fix it |
13 | | -//== How to fix it in FRAMEWORK NAME |
14 | 14 |
|
15 | | -=== Code examples |
| 15 | +Replace uses of out-of-scope features with standard-conforming alternatives that exist in the configured {cpp} standard. If the newer feature (or a removed feature) is truly required, upgrade (or align) the project’s configured standard and all toolchains consistently. |
16 | 16 |
|
17 | | -==== Noncompliant code example |
| 17 | +=== Defaulted comparison operators |
18 | 18 |
|
| 19 | +Defaulted comparison operators are introduced in {cpp}20. When the configured standard is earlier than {cpp}20, using defaulted comparison operators is noncompliant. Under {cpp}20 or later, the same code is compliant. |
| 20 | + |
| 21 | +==== Noncompliant code example |
19 | 22 | [source,cpp,diff-id=1,diff-type=noncompliant] |
20 | 23 | ---- |
21 | | -FIXME |
| 24 | +struct Account { |
| 25 | + int id; |
| 26 | + bool operator==(const Account&) const = default; // Noncompliant before {cpp}20 |
| 27 | +}; |
22 | 28 | ---- |
23 | 29 |
|
24 | 30 | ==== Compliant solution |
25 | | - |
26 | 31 | [source,cpp,diff-id=1,diff-type=compliant] |
27 | 32 | ---- |
28 | | -FIXME |
| 33 | +struct Account { |
| 34 | + int id; |
| 35 | + bool operator==(const Account& other) const { return id == other.id; } |
| 36 | +}; |
| 37 | +---- |
| 38 | + |
| 39 | +=== Using enum declaration |
| 40 | + |
| 41 | +The "using enum" declaration is a {cpp}20 feature. When the configured standard is earlier than {cpp}20, it is noncompliant to rely on it. Under {cpp}20 or later, the same code is compliant. |
| 42 | + |
| 43 | +==== Noncompliant code example |
| 44 | +[source,cpp,diff-id=2,diff-type=noncompliant] |
| 45 | +---- |
| 46 | +enum class Status { Ok, Error }; |
| 47 | +using enum Status; // Noncompliant before {cpp}20 |
| 48 | +auto s = Ok; |
| 49 | +---- |
| 50 | + |
| 51 | +==== Compliant solution |
| 52 | +[source,cpp,diff-id=2,diff-type=compliant] |
| 53 | +---- |
| 54 | +enum class Status { Ok, Error }; |
| 55 | +auto s = Status::Ok; |
| 56 | +---- |
| 57 | + |
| 58 | +=== Lambda template parameter list |
| 59 | + |
| 60 | +Lambda template parameter lists are available starting in {cpp}20. When the configured standard is earlier than {cpp}20, this usage is noncompliant. Under {cpp}20 or later, the same code is compliant. |
| 61 | + |
| 62 | +==== Noncompliant code example |
| 63 | +[source,cpp,diff-id=3,diff-type=noncompliant] |
| 64 | +---- |
| 65 | +auto mapValue = []<typename T>(T t) { return t; }; // Noncompliant before {cpp}20 |
| 66 | +---- |
| 67 | + |
| 68 | +==== Compliant solution |
| 69 | +[source,cpp,diff-id=3,diff-type=compliant] |
| 70 | +---- |
| 71 | +auto mapValue = [](auto t) { return t; }; |
| 72 | +---- |
| 73 | + |
| 74 | +=== C array designators in {cpp} |
| 75 | + |
| 76 | +Some C-only constructs, such as array designators, may be accepted by {cpp} compilers as extensions, but they are not part of the {cpp} standard and should be avoided in {cpp} code. |
| 77 | + |
| 78 | +==== Noncompliant code example |
| 79 | +[source,cpp,diff-id=4,diff-type=noncompliant] |
| 80 | +---- |
| 81 | +int values[3] = { [1] = 5 }; // Noncompliant in {cpp} |
| 82 | +---- |
| 83 | + |
| 84 | +==== Compliant solution |
| 85 | +[source,cpp,diff-id=4,diff-type=compliant] |
| 86 | +---- |
| 87 | +int values[3] = {}; |
| 88 | +values[1] = 5; |
| 89 | +---- |
| 90 | + |
| 91 | +=== Removed library feature |
| 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. |
| 94 | + |
| 95 | +==== Noncompliant code example |
| 96 | +[source,cpp,diff-id=5,diff-type=noncompliant] |
| 97 | +---- |
| 98 | +#include <memory> |
| 99 | +
|
| 100 | +std::auto_ptr<int> p(new int(42)); // Noncompliant in {cpp}17 and later |
| 101 | +---- |
| 102 | + |
| 103 | +==== Compliant solution |
| 104 | +[source,cpp,diff-id=5,diff-type=compliant] |
| 105 | +---- |
| 106 | +#include <memory> |
| 107 | +
|
| 108 | +std::unique_ptr<int> p = std::make_unique<int>(42); |
29 | 109 | ---- |
30 | 110 |
|
31 | | -//=== How does this work? |
| 111 | +=== Limitations |
32 | 112 |
|
33 | | -//=== Pitfalls |
| 113 | +This version primarily reports extensions starting from {cpp}20 and later. For example, it does not yet report use of a {cpp}17-only feature when the configured standard is {cpp}14. This limitation is temporary and will be addressed in a future update. |
34 | 114 |
|
35 | | -//=== Going the extra mile |
| 115 | +== Resources |
36 | 116 |
|
| 117 | +=== Related rules |
37 | 118 |
|
38 | | -//== Resources |
39 | | -//=== Documentation |
40 | | -//=== Articles & blog posts |
41 | | -//=== Conference presentations |
42 | | -//=== Standards |
43 | | -//=== External coding guidelines |
44 | | -//=== Benchmarks |
| 119 | +* S3715 - GNU extensions should not be used |
| 120 | +* S8230 - MSVC-specific extensions should not be used |
| 121 | +* S8231 - Non-standard attributes should not be used |
0 commit comments