Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 modern c++ for c programmers part 2.md #74

Merged

Conversation

hanxiaomax
Copy link
Member

@hanxiaomax hanxiaomax commented Aug 25, 2018

(提交翻译请使用下面的模板,其他类型pr请删除)

closes #22

1.译者信息

(转载时的署名信息,有需要请补充)

伯乐在线id 新浪微博
hanxiaomax

2.疑问区

(可在此区域列举有疑问的语句,提示校对人员重点关注)

One thing that seems promising is the std::expected work or boost::expected which creates functions that have both return codes or throw exceptions if you don’t look at them.

有点拿不准这句话的意思

3.自查表

  • 确认pr的目标分支是trans分支
  • 确认pr不包含除译文以外的文件
  • 补充评论区closes关键字以关联对应issue
  • 通读文章,确保读者(非译者)能够理解文章内容(语言性,非技术性)
  • 通读文章,确保语句通顺(有疑问语句请在疑问区提出)
  • 符合排版规范要求

4.发布信息(译者无需填写)

发布链接:


@hanxiaomax

@ghost ghost assigned hanxiaomax Aug 25, 2018
@ghost ghost added the B3-Need review 完成翻译待校对,请至对应的pr认领校对并添加校对中标签 label Aug 25, 2018
@hanxiaomax hanxiaomax requested a review from kele1997 August 25, 2018 14:05
@hanxiaomax hanxiaomax added T-C/C++ T-系列文章 所属系列见project labels Aug 30, 2018
Copy link
Member

@kele1997 kele1997 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该可以了😁


Like C11, C++ offers atomic operations. These are as simple as defining std::atomic<uint64_t> packetcounter. Operations on packetcounter are then atomic, with a wide suite of ways of interrogating or updating packetcounter if specific modes are required to for example build lock free data structures.

和像 C11 一样,C++ 提供了原子操作。就像定义 `std::atomic<uint64_t> packetcounter` 一样简单,对 `packetcounter` 的查询和更新就变成了原子操作,如果我们需要创建一无锁数据结构的话,这样做很方便。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“和像 C11 一样” -> “和” 与 “像” 重复了


Namespaces allow things with identical names to live side by side. This is of immediate relevance to us since C++ defines a lot of functions and classes that might collide with names you are already using in C. Because of this, the C++ libraries live in the std:: namespace, making it far easier to compile your C code as C++.

命名空间保证变量可以拥有唯一的变量名。这和我们息息相关是因为 C++ 定义了很多函数和类,以至于可能会有和 C 语言同名函数产生命名冲突的可能。因此 C++ 标准库被定义在`std::`命名空间中,这使得我们将 C 语言代码看作 C++ 代码进行编译变得更加简单。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Namespaces allow things with identical names to live side by side" 这句话感觉应该是这样:“命名空间允许相同命名的事物(变量,函数,类)同时存在。” 举个例子:std::string,其实我也可以自己创建命名空间,再写一个string,custom::string

这里显示了一个被 `SmartFP::SmartFP` 抛出的异常,这个异常“穿越”了 `func2()` 和 `func()`,然后在 `main()` 中被捕获。这种不断上抛异常的特性,其好的一方面是所有错误最终都会被关注到,而不像 return 可能会导致异常没有被处理。然而,它不好的一方面是捕获它的地方与抛出它的地方相距甚远,这可能让人感到困惑。这通常需要做到很好的日志记录。

Combined with RAII, exceptions are a very powerful technique to safely acquire resources and also deal with errors.
结合 RAII ,异常机制可以成为非常强大的计数,用于安全的获取资源并处理错误。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

计数 -> 技术


This technique to use classes or structs with constructors and destructors to own resources is called Resource Acquisition Is Initialization or RAII, and it is used widely. It is quite common for even larger C++ projects to not contain a single call to new or delete (or malloc/free) outside of a constructor/destructor pair. Or at all, in fact.

通过构造函数和析构函数来管理我们的资源,这种使用类或者结构体的技术就叫做:资源获取即初始化( Resource Acquisition Is Initialization )或 简称为RAII。这是一种被广泛使用的技术。对于大型 C++ 项目,使用这种方法可以保证不用单独调用 new 或者 delete (或 malloc/free)在构造和析构函数外申请和释放内存。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RAII 前后加一下空格


C++ offers other smart pointers, the most relevant of which is std::unique_ptr. Frequently we do not actually need actual reference counting but only ‘clean up if we go out of scope’. This is what std::unique_ptr offers, with literally zero overhead. There are also facilities for ‘moving’ a std::unique_ptr into storage so it stays in scope. We will get back to this later.

C++ 还提供了其他的智能指针,和前文介绍的智能指针最相关的是 `std::unique_ptr`。通常我们并不需要引用计数器,我们仅仅是希望在变量超出作用域时释放内存。 `std::unique_ptr` 可以提供这种能力而且没有任何额外的开销。同时,我们可以将 `std::unique_ptr` 移动到储存中,让其驻留在变量域中,这一点我们稍后会讨论。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“我们可以将 std::unique_ptr 移动到储存中,让其驻留在变量域中” 不太好理解
第五篇文章std::move 部分,实际上这里的 moving表示的是 std::move操作,根据原文 The semantics of a move are a true transfer of ownership ,这样翻译比较好: “我们可以转让 std::unique_ptr 的所有权,让它驻留在作用域中,”


And with this, we can now see why our earlier defined SmartFP is not very safe to use. It is possible to make a copy of it, and once that copy goes out of scope, it will ALSO close the same FILE*. std::shared_ptr saves us from having to think about thse things.

现在我们可以看到,为什么说之前我们定义的 `SmartFP` 指针并不安全。我们可以拷贝`SmartFP` 指针,当它的任意拷贝超出作用域时,都会关闭`FILE*`。`std::shared_ptr` 保证了我们不必担心这样的事情发生。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmartFPFILE* 的中英空格


This first defines a vector of std::shared_ptrs to Circle, then creates such a shared_ptr and stores it in the circles vector. When func returns, ptr goes out of scope, but since a copy of it is in the vector circles, the Circle object stays alive. std::shared_ptr is therefore a reference counting smart pointer.

首先我们创建一个容器,用于存放 `Circle` 实例的智能指针,然后我们创建一个`shared_ptr`并将其存放到容器中。当函数返回时,指针超过了它的作用域,但是因为它的一个拷贝还存放在容器中,`Circle` 对象仍然存在。`std::shared_ptr` 因此是一个引用计数智能指针。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared_ptr 中英空格

第二种形式不光可以少打一些字母,运行效率也更高。

std::shared_ptr however has more tricks up its sleeve:
然而,`std::shared_ptr`还可以有很多花式玩儿法:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::shared_ptr 中英空格


As noted, no error handling technique is perfect. One thing that seems promising is the std::expected work or boost::expected which creates functions that have both return codes or throw exceptions if you don’t look at them.

注意,没有什么异常处理技术是完美的。** `std::expected` 或 `boost::expected` 看上去很有前景,它们创建的函数既返回代码,也可以抛出异常。**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考1 参考2 both or ,推测return codes,这里的codes不是代码,实际上是一个携带了错误信息的 Expected 类。
“它们创建的函数既返回代码,也可以抛出异常。” -> “它们创建的函数可以返回错误信息,如果你不想查看错误信息,也可以直接抛出异常”

修改完毕
@hanxiaomax hanxiaomax added C1-Finished 校对完成,待发布 and removed B3-Need review 完成翻译待校对,请至对应的pr认领校对并添加校对中标签 labels Sep 2, 2018
@kele1997
Copy link
Member

kele1997 commented Sep 3, 2018

ok

@kele1997
Copy link
Member

kele1997 commented Sep 3, 2018

其实我觉得那个“转让所有权”还是不好,但我想不出别的了…

@hanxiaomax
Copy link
Member Author

我再研究下

@hanxiaomax hanxiaomax merged commit 7291b5a into jobbole:trans Sep 5, 2018
@hanxiaomax hanxiaomax deleted the #22-MODERN-C++-FOR-C-PROGRAMMERS-PART-2.md branch September 5, 2018 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C1-Finished 校对完成,待发布 T-系列文章 所属系列见project T-C/C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MODERN C++ FOR C PROGRAMMERS: PART 2
2 participants