mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Correct the examples to match guideline NL.16 (#1505)
This commit is contained in:
parent
b59bb13f2e
commit
148b1064da
|
@ -477,7 +477,6 @@ What is expressed in code has defined semantics and can (in principle) be checke
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class Date {
|
class Date {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
Month month() const; // do
|
Month month() const; // do
|
||||||
int month(); // don't
|
int month(); // don't
|
||||||
|
@ -4078,7 +4077,6 @@ An explicit distinction between interface and implementation improves readabilit
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class Date {
|
class Date {
|
||||||
// ... some representation ...
|
|
||||||
public:
|
public:
|
||||||
Date();
|
Date();
|
||||||
// validate that {yy, mm, dd} is a valid date and initialize
|
// validate that {yy, mm, dd} is a valid date and initialize
|
||||||
|
@ -4087,6 +4085,8 @@ An explicit distinction between interface and implementation improves readabilit
|
||||||
int day() const;
|
int day() const;
|
||||||
Month month() const;
|
Month month() const;
|
||||||
// ...
|
// ...
|
||||||
|
private:
|
||||||
|
// ... some representation ...
|
||||||
};
|
};
|
||||||
|
|
||||||
For example, we can now change the representation of a `Date` without affecting its users (recompilation is likely, though).
|
For example, we can now change the representation of a `Date` without affecting its users (recompilation is likely, though).
|
||||||
|
@ -5459,7 +5459,6 @@ To avoid unintended conversions.
|
||||||
##### Example, bad
|
##### Example, bad
|
||||||
|
|
||||||
class String {
|
class String {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
String(int); // BAD
|
String(int); // BAD
|
||||||
// ...
|
// ...
|
||||||
|
@ -5472,7 +5471,6 @@ To avoid unintended conversions.
|
||||||
If you really want an implicit conversion from the constructor argument type to the class type, don't use `explicit`:
|
If you really want an implicit conversion from the constructor argument type to the class type, don't use `explicit`:
|
||||||
|
|
||||||
class Complex {
|
class Complex {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
Complex(double d); // OK: we want a conversion from d to {d, 0}
|
Complex(double d); // OK: we want a conversion from d to {d, 0}
|
||||||
// ...
|
// ...
|
||||||
|
@ -6101,11 +6099,11 @@ A non-throwing move will be used more efficiently by standard-library and langua
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Vector {
|
class Vector {
|
||||||
// ...
|
public:
|
||||||
Vector(Vector&& a) noexcept :elem{a.elem}, sz{a.sz} { a.sz = 0; a.elem = nullptr; }
|
Vector(Vector&& a) noexcept :elem{a.elem}, sz{a.sz} { a.sz = 0; a.elem = nullptr; }
|
||||||
Vector& operator=(Vector&& a) noexcept { elem = a.elem; sz = a.sz; a.sz = 0; a.elem = nullptr; }
|
Vector& operator=(Vector&& a) noexcept { elem = a.elem; sz = a.sz; a.sz = 0; a.elem = nullptr; }
|
||||||
// ...
|
// ...
|
||||||
public:
|
private:
|
||||||
T* elem;
|
T* elem;
|
||||||
int sz;
|
int sz;
|
||||||
};
|
};
|
||||||
|
@ -6116,11 +6114,11 @@ These operations do not throw.
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Vector2 {
|
class Vector2 {
|
||||||
// ...
|
public:
|
||||||
Vector2(Vector2&& a) { *this = a; } // just use the copy
|
Vector2(Vector2&& a) { *this = a; } // just use the copy
|
||||||
Vector2& operator=(Vector2&& a) { *this = a; } // just use the copy
|
Vector2& operator=(Vector2&& a) { *this = a; } // just use the copy
|
||||||
// ...
|
// ...
|
||||||
public:
|
private:
|
||||||
T* elem;
|
T* elem;
|
||||||
int sz;
|
int sz;
|
||||||
};
|
};
|
||||||
|
@ -6356,7 +6354,6 @@ A `swap` can be handy for implementing a number of idioms, from smoothly moving
|
||||||
##### Example, good
|
##### Example, good
|
||||||
|
|
||||||
class Foo {
|
class Foo {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
void swap(Foo& rhs) noexcept
|
void swap(Foo& rhs) noexcept
|
||||||
{
|
{
|
||||||
|
@ -9094,20 +9091,20 @@ The `unique_ptr` protects against leaks by guaranteeing the deletion of its obje
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class X {
|
class X {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
T* p; // bad: it is unclear whether p is owning or not
|
T* p; // bad: it is unclear whether p is owning or not
|
||||||
T* q; // bad: it is unclear whether q is owning or not
|
T* q; // bad: it is unclear whether q is owning or not
|
||||||
|
// ...
|
||||||
};
|
};
|
||||||
|
|
||||||
We can fix that problem by making ownership explicit:
|
We can fix that problem by making ownership explicit:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class X2 {
|
class X2 {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
owner<T*> p; // OK: p is owning
|
owner<T*> p; // OK: p is owning
|
||||||
T* q; // OK: q is not owning
|
T* q; // OK: q is not owning
|
||||||
|
// ...
|
||||||
};
|
};
|
||||||
|
|
||||||
##### Exception
|
##### Exception
|
||||||
|
@ -17454,7 +17451,6 @@ Flag uses where an explicitly specialized type exactly matches the types of the
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class X {
|
class X {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
explicit X(int);
|
explicit X(int);
|
||||||
X(const X&); // copy
|
X(const X&); // copy
|
||||||
|
@ -21749,7 +21745,6 @@ If you define a move constructor, you must also define a move assignment operato
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
class X {
|
class X {
|
||||||
// ...
|
|
||||||
public:
|
public:
|
||||||
X(const X&) { /* stuff */ }
|
X(const X&) { /* stuff */ }
|
||||||
|
|
||||||
|
@ -21758,6 +21753,8 @@ If you define a move constructor, you must also define a move assignment operato
|
||||||
X(x&&) noexcept { /* stuff */ }
|
X(x&&) noexcept { /* stuff */ }
|
||||||
|
|
||||||
// BAD: failed to also define a move assignment operator
|
// BAD: failed to also define a move assignment operator
|
||||||
|
|
||||||
|
// ...
|
||||||
};
|
};
|
||||||
|
|
||||||
X x1;
|
X x1;
|
||||||
|
@ -21841,10 +21838,10 @@ Prevent leaks. Leaks can lead to performance degradation, mysterious error, syst
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class Vector {
|
class Vector {
|
||||||
// ...
|
|
||||||
private:
|
private:
|
||||||
T* elem; // sz elements on the free store, owned by the class object
|
T* elem; // sz elements on the free store, owned by the class object
|
||||||
int sz;
|
int sz;
|
||||||
|
// ...
|
||||||
};
|
};
|
||||||
|
|
||||||
This class is a resource handle. It manages the lifetime of the `T`s. To do so, `Vector` must define or delete [the set of special operations](???) (constructors, a destructor, etc.).
|
This class is a resource handle. It manages the lifetime of the `T`s. To do so, `Vector` must define or delete [the set of special operations](???) (constructors, a destructor, etc.).
|
||||||
|
|
Loading…
Reference in New Issue
Block a user