mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
travis CI fixes
This commit is contained in:
@@ -2162,7 +2162,7 @@ We might write
|
|||||||
}
|
}
|
||||||
istream& in = *inp;
|
istream& in = *inp;
|
||||||
|
|
||||||
This violated the ruly [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
|
||||||
@@ -2171,27 +2171,27 @@ In particular, someone has to remember to somewhere write
|
|||||||
|
|
||||||
We could handle this particular example by using `unique_ptr` with a special deleter that does nothing for `cin`,
|
We could handle this particular example by using `unique_ptr` with a special deleter that does nothing for `cin`,
|
||||||
but that's complicated for novices (who can easily encounter this problem) and the example is an example of a more general
|
but that's complicated for novices (who can easily encounter this problem) and the example is an example of a more general
|
||||||
problem where a property that we would like to consider static (here, ownership) needs infrequesntly be addressed
|
problem where a property that we would like to consider static (here, ownership) needs infrequently be addressed
|
||||||
at run time.
|
at run time.
|
||||||
The common, most frequent, and safest examples can be handled statically, so we don't want to add cost and complexity to those.
|
The common, most frequent, and safest examples can be handled statically, so we don't want to add cost and complexity to those.
|
||||||
But we must also cope with the uncommon, less-safe, and necessarily more expensive cases.
|
But we must also cope with the uncommon, less-safe, and necessarily more expensive cases.
|
||||||
Such examples are discussed in [[Str15]](http://www.stroustrup.com/resource-model.pdf).
|
Such examples are discussed in [[Str15]](http://www.stroustrup.com/resource-model.pdf).
|
||||||
|
|
||||||
So, we write a class
|
So, we write a class
|
||||||
|
|
||||||
class Istream { [[gsl::suppress(lifetime)]]
|
class Istream { [[gsl::suppress(lifetime)]]
|
||||||
public:
|
public:
|
||||||
enum Opt { from_line=1 };
|
enum Opt { from_line = 1 };
|
||||||
Istream() { }
|
Istream() { }
|
||||||
Istream(zstring p) :owned{true}, inp{new ifstream{p}} {} // read from file
|
Istream(zstring p) :owned{true}, inp{new ifstream{p}} {} // read from file
|
||||||
Istream(zstring p,Opt) :owned{true}, inp{new istringstream{p}} {} // read from command line
|
Istream(zstring p, Opt) :owned{true}, inp{new istringstream{p}} {} // read from command line
|
||||||
~Itream() { if (owned) delete inp; }
|
~Itream() { if (owned) delete inp; }
|
||||||
operator istream& () { return *inp; }
|
operator istream& () { return *inp; }
|
||||||
private:
|
private:
|
||||||
bool owned = false;
|
bool owned = false;
|
||||||
istream* inp = &cin;
|
istream* inp = &cin;
|
||||||
};
|
};
|
||||||
|
|
||||||
Now, the dynamic nature of `istream` ownership has been encapsulated.
|
Now, the dynamic nature of `istream` ownership has been encapsulated.
|
||||||
Presumably, a bit of checking for potential errors would be added in real code.
|
Presumably, a bit of checking for potential errors would be added in real code.
|
||||||
|
|
||||||
@@ -6114,7 +6114,7 @@ This is not just slow, but if a memory allocation occurs for the elements in `tm
|
|||||||
|
|
||||||
(Simple) When a class has a `swap` member function, it should be declared `noexcept`.
|
(Simple) When a class has a `swap` member function, it should be declared `noexcept`.
|
||||||
|
|
||||||
### <a name="Rc-swap-noexcept"></a>: C.85: Make `swap` `noexcept`
|
### <a name="Rc-swap-noexcept"></a>C.85: Make `swap` `noexcept`
|
||||||
|
|
||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
@@ -7134,7 +7134,7 @@ This rule is about using `final` on classes with virtual functions meant to be i
|
|||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
Capping an individual virtual function with `final` is error-prone as `final` can easily be overlooked when defining/overriding a set of functions.
|
Capping an individual virtual function with `final` is error-prone as `final` can easily be overlooked when defining/overriding a set of functions.
|
||||||
Fortunately, the compiler catches such mistakes: You cannot re-declare/re-open a `final` member a derived class.
|
Fortunately, the compiler catches such mistakes: You cannot re-declare/re-open a `final` member in a derived class.
|
||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
@@ -11889,7 +11889,7 @@ This also applies to `%`.
|
|||||||
Choosing `unsigned` implies many changes to the usual behavior of integers, including modulo arithmetic,
|
Choosing `unsigned` implies many changes to the usual behavior of integers, including modulo arithmetic,
|
||||||
can suppress warnings related to overflow,
|
can suppress warnings related to overflow,
|
||||||
and opens the door for errors related to signed/unsigned mixes.
|
and opens the door for errors related to signed/unsigned mixes.
|
||||||
Using `unsigned` doesn't actually eliminate the possibility of negative values.
|
Using `unsigned` doesn't actually eliminate the possibility of negative values.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
@@ -11904,32 +11904,32 @@ Consider:
|
|||||||
unsigned area(unsigned height, unsigned width) { return height*width; } // [see also](#Ri-expects)
|
unsigned area(unsigned height, unsigned width) { return height*width; } // [see also](#Ri-expects)
|
||||||
// ...
|
// ...
|
||||||
int height;
|
int height;
|
||||||
cin>>height;
|
cin >> height;
|
||||||
auto a = area(height,2); // if the input is -2 a becomes 4294967292
|
auto a = area(height, 2); // if the input is -2 a becomes 4294967292
|
||||||
|
|
||||||
Remember that `-1` when assigned to an `unsigned int` becomes the largest `unsigned int`.
|
Remember that `-1` when assigned to an `unsigned int` becomes the largest `unsigned int`.
|
||||||
Also, since unsigned arithmetic is modulo arithmentic the multiplication didn't overflow, it wrapped around.
|
Also, since unsigned arithmetic is modulo arithmetic the multiplication didn't overflow, it wrapped around.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
unsigned max = 100000; // "accidental typo", I mean to say 10'000
|
unsigned max = 100000; // "accidental typo", I mean to say 10'000
|
||||||
unsigned short x = 100;
|
unsigned short x = 100;
|
||||||
while (x<max) x += 100; // infinite loop
|
while (x < max) x += 100; // infinite loop
|
||||||
|
|
||||||
Had `x` been a signed `short`, we could have warned about the undefined behavior upon overflow.
|
Had `x` been a signed `short`, we could have warned about the undefined behavior upon overflow.
|
||||||
|
|
||||||
##### Alternatives
|
##### Alternatives
|
||||||
|
|
||||||
* use signed integers and check for `x>= 0`
|
* use signed integers and check for `x >= 0`
|
||||||
* use a positive integer type
|
* use a positive integer type
|
||||||
* use an integer subrange type
|
* use an integer subrange type
|
||||||
* `Assert(-1<x)`
|
* `Assert(-1 < x)`
|
||||||
|
|
||||||
For example
|
For example
|
||||||
|
|
||||||
struct Positive {
|
struct Positive {
|
||||||
int val;
|
int val;
|
||||||
Positive(int x) :val{x} { Assert(0<x); }
|
Positive(int x) :val{x} { Assert(0 < x); }
|
||||||
operator int() { return val; }
|
operator int() { return val; }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -11957,17 +11957,21 @@ To enable better error detection.
|
|||||||
|
|
||||||
##### Example, bad
|
##### Example, bad
|
||||||
|
|
||||||
vector<int> vec {1,2,3,4,5};
|
vector<int> vec {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
for (int i=0; i<vec.size(); i+=2) cout << vec[i] << '\n'; // mix int and unsigned
|
for (int i=0; i < vec.size(); i+=2) // mix int and unsigned
|
||||||
for (unsigned i=0; i<vec.size(); i+=2) cout << vec[i] << '\n'; // risk wraparound
|
cout << vec[i] << '\n';
|
||||||
for (vector<int>::size_type i=0; i<vec.size(); i+=2) cout << vec[i] << '\n'; // verbose
|
for (unsigned i=0; i < vec.size(); i+=2) // risk wraparound
|
||||||
for (auto i=0; i<vec.size(); i+=2) cout << vec[i] << '\n'; // mix int and unsigned
|
cout << vec[i] << '\n';
|
||||||
|
for (vector<int>::size_type i=0; i < vec.size(); i+=2) // verbose
|
||||||
|
cout << vec[i] << '\n';
|
||||||
|
for (auto i=0; i < vec.size(); i+=2) // mix int and unsigned
|
||||||
|
cout << vec[i] << '\n';
|
||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
The built-in array uses signed subscripts.
|
The built-in array uses signed subscripts.
|
||||||
The standard-library containers use unsigned sunscripts.
|
The standard-library containers use unsigned subscripts.
|
||||||
Thus, no perfect and fully compatible solution is possible.
|
Thus, no perfect and fully compatible solution is possible.
|
||||||
Given the known problems with unsigned and signed/unsigned mixtures, better stick to (signed) integers.
|
Given the known problems with unsigned and signed/unsigned mixtures, better stick to (signed) integers.
|
||||||
|
|
||||||
@@ -14266,7 +14270,7 @@ Instead, use a reference:
|
|||||||
|
|
||||||
catch (exception& e) { /* ... */ }
|
catch (exception& e) { /* ... */ }
|
||||||
|
|
||||||
of - typically better stil - a `const` reference:
|
of - typically better still - a `const` reference:
|
||||||
|
|
||||||
catch (const exception& e) { /* ... */ }
|
catch (const exception& e) { /* ... */ }
|
||||||
|
|
||||||
@@ -17545,15 +17549,15 @@ Avoid accidentally becoming dependent on implementation details and logically se
|
|||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
#include<iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void use() // bad
|
void use() // bad
|
||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
cin >> s; // fine
|
cin >> s; // fine
|
||||||
getline(cin,s); // error: getline() not defined
|
getline(cin, s); // error: getline() not defined
|
||||||
if (s=="surprise") { // error == not defined
|
if (s == "surprise") { // error == not defined
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17565,16 +17569,16 @@ or even an occasional "`string`s cannot be compared with `==`).
|
|||||||
|
|
||||||
The solution is to explicitly `#include<string>`:
|
The solution is to explicitly `#include<string>`:
|
||||||
|
|
||||||
#include<iostream>
|
#include <iostream>
|
||||||
#include<string>
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void use()
|
void use()
|
||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
cin >> s; // fine
|
cin >> s; // fine
|
||||||
getline(cin,s); // fine
|
getline(cin, s); // fine
|
||||||
if (s=="surprise") { // fine
|
if (s == "surprise") { // fine
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17586,12 +17590,12 @@ For example:
|
|||||||
|
|
||||||
// basic_std_lib.h:
|
// basic_std_lib.h:
|
||||||
|
|
||||||
#include<vector>
|
#include <vector>
|
||||||
#include<string>
|
#include <string>
|
||||||
#include<map>
|
#include <map>
|
||||||
#include<iostream>
|
#include <iostream>
|
||||||
#include<random>
|
#include <random>
|
||||||
#include<vector>
|
#include <vector>
|
||||||
|
|
||||||
a user can now get that set of declarations with a single `#include`"
|
a user can now get that set of declarations with a single `#include`"
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
'
|
'
|
||||||
|
10'000
|
||||||
0xFF0000
|
0xFF0000
|
||||||
0b0101'0101
|
0b0101'0101
|
||||||
10x
|
10x
|
||||||
@@ -184,6 +185,7 @@ g1
|
|||||||
g2
|
g2
|
||||||
GCC
|
GCC
|
||||||
Geosoft
|
Geosoft
|
||||||
|
getline
|
||||||
getx
|
getx
|
||||||
GFM
|
GFM
|
||||||
Girou
|
Girou
|
||||||
@@ -205,7 +207,9 @@ hnd
|
|||||||
homebrew
|
homebrew
|
||||||
HPL
|
HPL
|
||||||
href
|
href
|
||||||
|
HTTP
|
||||||
Hyslop
|
Hyslop
|
||||||
|
i2
|
||||||
IDE
|
IDE
|
||||||
IDEs
|
IDEs
|
||||||
IEC
|
IEC
|
||||||
@@ -480,6 +484,7 @@ stmt
|
|||||||
str
|
str
|
||||||
strdup
|
strdup
|
||||||
strlen
|
strlen
|
||||||
|
Str15
|
||||||
Stroustrup
|
Stroustrup
|
||||||
Stroustrup00
|
Stroustrup00
|
||||||
Stroustrup05
|
Stroustrup05
|
||||||
@@ -527,6 +532,8 @@ typeid
|
|||||||
typename
|
typename
|
||||||
typesafe
|
typesafe
|
||||||
UB
|
UB
|
||||||
|
u1
|
||||||
|
u2
|
||||||
unaliased
|
unaliased
|
||||||
uncompromised
|
uncompromised
|
||||||
underuse
|
underuse
|
||||||
|
|||||||
Reference in New Issue
Block a user