mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-18 21:24:41 +03:00
updating pages
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
---
|
---
|
||||||
layout: default
|
layout: default
|
||||||
---
|
---
|
||||||
|
|
||||||
# <a name="main"></a>C++ Core Guidelines
|
# <a name="main"></a>C++ Core Guidelines
|
||||||
|
|
||||||
November 27, 2017
|
December 11, 2017
|
||||||
|
|
||||||
|
|
||||||
Editors:
|
Editors:
|
||||||
@@ -86,7 +85,7 @@ You can sample rules for specific language features:
|
|||||||
* `concept`:
|
* `concept`:
|
||||||
[rules](#SS-concepts) --
|
[rules](#SS-concepts) --
|
||||||
[in generic programming](#Rt-raise) --
|
[in generic programming](#Rt-raise) --
|
||||||
[template arguments](#RT-concepts) --
|
[template arguments](#Rt-concepts) --
|
||||||
[semantics](#Rt-low)
|
[semantics](#Rt-low)
|
||||||
* constructor:
|
* constructor:
|
||||||
[invariant](#Rc-struct) --
|
[invariant](#Rc-struct) --
|
||||||
@@ -1194,7 +1193,7 @@ Having good (easy-to-understand, encouraging efficient use, not error-prone, sup
|
|||||||
Interface rule summary:
|
Interface rule summary:
|
||||||
|
|
||||||
* [I.1: Make interfaces explicit](#Ri-explicit)
|
* [I.1: Make interfaces explicit](#Ri-explicit)
|
||||||
* [I.2: Avoid global variables](#Ri-global)
|
* [I.2: Avoid non-`const` global variables](#Ri-global)
|
||||||
* [I.3: Avoid singletons](#Ri-singleton)
|
* [I.3: Avoid singletons](#Ri-singleton)
|
||||||
* [I.4: Make interfaces precisely and strongly typed](#Ri-typed)
|
* [I.4: Make interfaces precisely and strongly typed](#Ri-typed)
|
||||||
* [I.5: State preconditions (if any)](#Ri-pre)
|
* [I.5: State preconditions (if any)](#Ri-pre)
|
||||||
@@ -1268,7 +1267,7 @@ Functions can be template functions and sets of functions can be classes or clas
|
|||||||
* (Simple) A function should not make control-flow decisions based on the values of variables declared at namespace scope.
|
* (Simple) A function should not make control-flow decisions based on the values of variables declared at namespace scope.
|
||||||
* (Simple) A function should not write to variables declared at namespace scope.
|
* (Simple) A function should not write to variables declared at namespace scope.
|
||||||
|
|
||||||
### <a name="Ri-global"></a>I.2: Avoid global variables
|
### <a name="Ri-global"></a>I.2: Avoid non-`const` global variables
|
||||||
|
|
||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
@@ -1740,7 +1739,7 @@ Many traditional interface functions (e.g., UNIX signal handlers) use error code
|
|||||||
|
|
||||||
##### Alternative
|
##### Alternative
|
||||||
|
|
||||||
If you can't use exceptions (e.g. because your code is full of old-style raw-pointer use or because there are hard-real-time constraints), consider using a style that returns a pair of values:
|
If you can't use exceptions (e.g., because your code is full of old-style raw-pointer use or because there are hard-real-time constraints), consider using a style that returns a pair of values:
|
||||||
|
|
||||||
int val;
|
int val;
|
||||||
int error_code;
|
int error_code;
|
||||||
@@ -1793,7 +1792,7 @@ Consider:
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Who deletes the returned `X`? The problem would be harder to spot if compute returned a reference.
|
Who deletes the returned `X`? The problem would be harder to spot if `compute` returned a reference.
|
||||||
Consider returning the result by value (use move semantics if the result is large):
|
Consider returning the result by value (use move semantics if the result is large):
|
||||||
|
|
||||||
vector<double> compute(args) // good
|
vector<double> compute(args) // good
|
||||||
@@ -2223,7 +2222,7 @@ We might write
|
|||||||
|
|
||||||
This violated the rule [against uninitialized variables](#Res-always),
|
This violated the rule [against uninitialized variables](#Res-always),
|
||||||
the rule against [ignoring ownership](#Ri-raw),
|
the rule against [ignoring ownership](#Ri-raw),
|
||||||
and the rule [against magic constants](#Res-magic) .
|
and the rule [against magic constants](#Res-magic).
|
||||||
In particular, someone has to remember to somewhere write
|
In particular, someone has to remember to somewhere write
|
||||||
|
|
||||||
if (owned) delete inp;
|
if (owned) delete inp;
|
||||||
@@ -2257,7 +2256,7 @@ Presumably, a bit of checking for potential errors would be added in real code.
|
|||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
* Hard, it is hard to decide what rule-breaking code is essential
|
* Hard, it is hard to decide what rule-breaking code is essential
|
||||||
* flag rule suppression that enable rule-violations to cross interfaces
|
* Flag rule suppression that enable rule-violations to cross interfaces
|
||||||
|
|
||||||
# <a name="S-functions"></a>F: Functions
|
# <a name="S-functions"></a>F: Functions
|
||||||
|
|
||||||
@@ -5972,6 +5971,7 @@ To prevent slicing, because the normal copy operations will copy only the base p
|
|||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class B { // GOOD: base class suppresses copying
|
class B { // GOOD: base class suppresses copying
|
||||||
|
public:
|
||||||
B(const B&) = delete;
|
B(const B&) = delete;
|
||||||
B& operator=(const B&) = delete;
|
B& operator=(const B&) = delete;
|
||||||
virtual unique_ptr<B> clone() { return /* B object */; }
|
virtual unique_ptr<B> clone() { return /* B object */; }
|
||||||
@@ -6093,6 +6093,8 @@ A `unique_ptr` can be moved, but not copied. To achieve that its copy operations
|
|||||||
auto pi3 {make()}; // OK, move: the result of make() is an rvalue
|
auto pi3 {make()}; // OK, move: the result of make() is an rvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Note that deleted methods should be public.
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
The elimination of a default operation is (should be) based on the desired semantics of the class. Consider such classes suspect, but maintain a "positive list" of classes where a human has asserted that the semantics is correct.
|
The elimination of a default operation is (should be) based on the desired semantics of the class. Consider such classes suspect, but maintain a "positive list" of classes where a human has asserted that the semantics is correct.
|
||||||
@@ -12195,8 +12197,9 @@ If you really need to break out a loop, a `break` is typically better than alter
|
|||||||
break;
|
break;
|
||||||
case Warning:
|
case Warning:
|
||||||
write_event_log();
|
write_event_log();
|
||||||
|
// Bad - implicit fallthrough
|
||||||
case Error:
|
case Error:
|
||||||
display_error_window(); // Bad
|
display_error_window();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12210,7 +12213,7 @@ It is easy to overlook the fallthrough. Be explicit:
|
|||||||
write_event_log();
|
write_event_log();
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case Error:
|
case Error:
|
||||||
display_error_window(); // Bad
|
display_error_window();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12224,7 +12227,7 @@ In C++17, use a `[[fallthrough]]` annotation:
|
|||||||
write_event_log();
|
write_event_log();
|
||||||
[[fallthrough]]; // C++17
|
[[fallthrough]]; // C++17
|
||||||
case Error:
|
case Error:
|
||||||
display_error_window(); // Bad
|
display_error_window();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16291,7 +16294,7 @@ The ability to specify a meaningful semantics is a defining characteristic of a
|
|||||||
&& has_multiply<T>
|
&& has_multiply<T>
|
||||||
&& has_divide<T>;
|
&& has_divide<T>;
|
||||||
|
|
||||||
template<Number N> auto algo(const N& a, const N& b) // use two numbers
|
template<Number N> auto algo(const N& a, const N& b)
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
return a + b;
|
return a + b;
|
||||||
@@ -17869,6 +17872,7 @@ If you intend for a class to match a concept, verifying that early saves users p
|
|||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class X {
|
class X {
|
||||||
|
public:
|
||||||
X() = delete;
|
X() = delete;
|
||||||
X(const X&) = default;
|
X(const X&) = default;
|
||||||
X(X&&) = default;
|
X(X&&) = default;
|
||||||
|
|||||||
Reference in New Issue
Block a user