A Tour of C++ (C++ In-Depth Series)
- Addison-Wesley Professional (2018年6月29日発売)
- Amazon.co.jp ・洋書 (256ページ)
- / ISBN・EAN: 9780134997834
感想・レビュー・書評
-
2021年01月24日 00時52分23秒 終了
もっとC++17やC++20について書かれているものかと期待していたのだけどそうでもなかった。基本は「プログラミング言語C++ 第4版」(の原著)に沿って書かれていて、新しい項目は付加的に書かれている感じだった。第4版を読んだ直後だったのでサクサク読めてすんなり頭に入ってきた。セクションのはじめの引用文は第4版のものそのままだし、文章も第4版から流用している部分が多かった。例えば「I picked the term "Classic C" from a sticker that used to be affixed to Dennis Richie's terminal.」などの小ネタは第4版にも書かれていた。簡単に言うと第4版をコンパクトにまとめて新しい項目をいくつか追加したものということになる。こちらは気軽に読めるので両方もっていても損はないと思った。
1 The Basics
------------
2 User-Defined Types
--------------------
P26
std::variant
3 Modularity
------------
P32
C++20 module
importやexportは使えない。
-std=c++2a、-std=c++20、-std=gnu++2a、-std=gnu++20
GCC8、GCC10
P37
invariant
class invariant
P40
Don't belive that all error codes or all exceptions are bad;
Furthermore, do not believe the myth that exception handling is slow;
P44
move for function return value
P45
Structured Binding
auto [n, v] = read_entry(is);
P46
[13] If in doubt whether to use an exception or an error code, prefer exceptions.
4 Classes
---------
P48
Concreate classes
Abstract classes
Classes in class hierarchies
P57
This virtual call mechanism can be made alomost as efficient as the "normal function call" mechanism (within 25%).
25%って結構でかいんじゃない?
P60
Interface inheritance
Implementation inheritance
P62
dynamic_castを使うケース
This typically happens when we pass an object to some system that accepts an interface specified by a base class. Whne that system later passes the object back to us, we might have to recover the original type.
P63
unique_ptrを使うように変更した場合、_allがvector<unique_ptr<Shape>>を受け付けるように変更しないといけない。
これは面倒くさいので§6.3.2で代替案を示す。
どうも関数オブジェクトを使うようにするのようだが、直接的な記述はなさそうに見える。
5 Essential Operations
----------------------
P73
Garbage collection
Do not litter!
P76
User-Defined Literals; UDL
6 Templates
-----------
P87
for_all
P91
Compile-Time if
template <typename T>
void update(T& target)
{
if constexpr(is_pod<T>::value)
simple_and_fast(target);
else
slow_and_safe(target);
}
7 Concepts and Generic Programming
----------------------------------
P100
lifting
The process of generalizing from a concrete piece of code (and preferably from several) while preserving performance is called lifting.
P101
parameter pack
template <typename T, typename... Tail>
void print(T head, Tail... tail)
{
cout << head << ' ';
if constexpr(sizeof...(tail) > 0)
print(tail...);
}
9 Strings and Regular Expressions
---------------------------------
P114
string_view
P115
gsl::span, gsl::string_span
P118
lazy match
non-greedy match
P121
(\d*)(\s(\w+))+ これは2つのグループ(グループはネストしない)
と書かれているが、smatchの結果にはちゃんとキャプチャされて入っている。
ネストしないとはどういう意味で言っているのだろう?
P121
A regex_iterator is a bidrectional iterator, so we cannot directly iterate over an istream (which offers only an input iterator).
bidirectionalだからistreamには使えないとはどういうこと?
なぜここでistreamの話が出てくる?
P113
std::stringのメモリレイアウトには文字列が終端文字'\0'を含むように描かれている。
これは実際に使わている形式と考えてよいのだろうか。
std:stringが'\0'を保持しているのかどうかずっと疑問だった。
その答えになっているだろうか。
10 Input and Output
-------------------
P131
テンプレートのデフォルト引数は、右詰めでなくても良いようだ。
template <typename Target = string, typename Source>
Target to(Source arg) { ... }
のようにすることもできる。
推論との組み合わせも、右からでなくても可能。
auto x = to(1.23); // Target はデフォルトのstring、Sourceはdoubleに推論される。
P132
ファイルシステムが扱うUnicodeはcppreferenceとBoostのfilesystemのドキュメントを参考にする。
11 Containers
-------------
P141
範囲チェックvector
template <typename T>
class Vec: public std::vector<T> {
public:
using vector<T>::vector;
T& operator[](int i) { return vector<T>::at(i); }
const T& operator[](int i) const { return vector<T>::at(i); }
};
P147
emplace_back()
for a vector<pair<int, string>>
v.push_back(pair{1, "copy or move"}); // make a pair and move it into v
v.empolace_back(1, "build in place"); // build a pair in v
P148
[15] User foward_list for sequences that are usually empth; §11.6.
P148
[20] For a container, user the ()-initializer syntax for sizes and the {}-initializer syntax for lists of elements; §4.2.3, §11.2.
12 Algorithms
-------------
P154
*ooと++ooは何もしない。
単に自分自身へのリファレンスを返す。
コンテナのイテレータの動作を真似るためにこのような演算子が用意されている。
そのためアルゴリズムが適用できる。
P156
使ったことのないアルゴリズム
p = move(b, e, out)
p = unique_copy(b, e, out)
(p1, p2) = equal_range(b, e, v)
p = merge(b, e, b2, e2, out)
p = merge(b, e, b2, e2, out, f)
P160
Container Algorithms
namespace Estd {
using namespace std;
template <typename C>
void sort(C& c) { sort(c.begin(), c.end()); }
template <typename C, typename Pred>
void sort(C& c, Pred p) { sort(c.begin(), c.end(), p); }
}
13 Utilities
------------
P166
Using make_shared() is not just more convenient than ...
make_shared()はオブジェクトの生成と参照カウンタの生成を同時に行うため、newを使ってsahred_ptrを初期化するよりも効率が良い。
P166
Given unique_ptr and shared_ptr, we can implement a complete "no naked new" policy for many program.
しかしスマートポインタは第2の選択肢にするべき。スマートであってもポインタには変わりない。
もっと高いコンセプトのレベルでのルールを適用するのが良い、コンテナなど。
P167
std::move()を使いすぎるのはエラーにつながりやすい。
P168
perfect forwarding
P168
string_spanは〜と書かれていて図はspan<int>になっている。どういうこと?
P169
gsl::span
P169
なぜ明示的にspanを組み立てるようにするとプログラマが誤る可能性が小さくなるのか?
P173
auto [first, last] = equal_range(v.begin(), v.end(), Record{"Reg"}, less);
P174
コンストラクタでのテンプレート引数の推論はC++17から。
古いコードではmake_tapleを使わざるを得なかった。
P176
variantとvisiorの比較。
P177
variant
optional
any
これらはすべてC++17で導入された。
P178
free store (also called, dynamic memory or heap)
フリーストアの別の呼び方。
P178
pmr ("Polymorphic Memory Resource")
16GBのメモリー消費が3MBにまで抑えられる、とある。
しかしなぜそうなるのか全くわからない。
詳しく調べる必要あり。
P179
auto spring_day = apr/7/2018;
cout << weekday(spring_day) << '\n';
It even handles leap seconds.
閏秒って何?
P181
functionには実行時のオーバーヘッドがある。
overloadedって何?
P182
タグディスパッチ tag dispatch
P184
compile-time if => P91
P184
enable_ifはSFINAEを使っている。
P185
[11] Don't use std::move; §13.2.2
14 Numerics
-----------
P188
sinh, cosh, tanh
Hyperbolic sine/cosine/tangent
ハイボリックてなんなのか前から気になっていた。
いまだに分からない、調べてない。
P188
数学関数のエラーはerrorから取得できる。
<cerrno>
EDOM: a domain error
ERANGE: a range error
P188
<cmath>で定義される数学関数が
standard mathematical functions
と呼ばれるのに対して、<cstdlib>で定義される数学関数は、
special mathematical functions
と呼ばれる。
beta(), rieman_zeta(), sph_bessel(),
しかし、<cmath>でも定義される。
なのでヘッダによって区別されるわけではなく、関数の性質によるものと考えるのが良いだろう。
P190
並列数値アルゴリズム
当面使うことはないだろうからあまり深入りしないで先に進む。
P191
default_random_engine
uniform_inst_distribution
P192
唯一ここだけ'\n'ではなく、endlが使われている。
なにか意図があるのだろうか?
15 Concurrency
--------------
P199
scoped_lock C++17
enabling us to acquire several locks simultaneously.
scoped_lock lck{mutex1, mutex2, mutex3};
P200
communicating through shared data is pretty low level.
Don't choose shared data for communication...
P200
reader-writer lock idiom
shared_lock C++14
unique_lock C++11
lock_guard C++11
scoped_lockとlock_guardの違いが分からない。
P201
scoped_lockではなくunique_lockを使用する場合の理由が挙げられている。
P203
current_exception()
promise<T>とfuture<T>はどうやって結び付けられるのだろう?
packaged_task::get_future()
async()
promise::get_future()
https://en.cppreference.com/w/cpp/thread/future
P204
packaged_taskがコピー禁止になっている理由
it is a resource handle: it owns its promise and is (indirectly) responsible for whatever resources its task may own.
16 History and Compatibility
----------------------------
P209
variable template C++14
https://en.cppreference.com/w/cpp/language/variable_template
https://cpprefjp.github.io/lang/cpp14/variable_templates.html
P209
C++ Core Guidelines
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
P219
"I picked the term "Classic C" from a sticker that used to be affixed to Dennis Richie's terminal."詳細をみるコメント0件をすべて表示