返璞归真:现代C++精要

参考

这是 Back to the Basics: Essentials of Modern C++ 的视频总结。

要点

1. 使用 range 进行迭代

for (auto& e : c) {...}

没有特别需要说明的。

2. 避免使用 new 和 delete

在需要使用 new 创建对象的场合,使用 unique_ptr 代替。例如:

#include <iostream>
#include <string>
#include <utility>

namespace c14 {
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args) {
  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

} // namespace c14

class Person {
public:
  explicit Person(const std::string &name, int age) {
    name_ = name;
    age_ = age;
    std::cout << "Created with lvalue" << std::endl;
  }

  explicit Person(const std::string &&name, int age) {
    name_ = std::move(name);
    age_ = age;
    std::cout << "Created with rvalue" << std::endl;
  }

  void speak() {
    std::cout << "Hello, I'm " << name_ << ", " << age_ << " years old."
              << std::endl;
  }

  ~Person() { std::cout << name_ << " deleted..." << std::endl; }

private:
  std::string name_;
  int age_;
};

int main(int argc, char const *argv[]) {
  auto p = c14::make_unique<Person>("yy", 27);
  p->speak();
  return 0;
}

/* 输出:
Created with rvalue
Hello, I'm yy, 27 years old.
yy deleted...
*/

尽量使用 unique_ptr 来管理对象的所有权,当确定某个对象需要共享则使用 shared_ptr。

  1. 它的开销和裸指针一样
  2. 它是 exception-safe 的,即使发生异常也能顺利析构
  3. 无需进行麻烦的引用计数

以上例子有两个值得注意的地方。

  1. std::make_unique 是 C++14 引入的特性。但是我们这里为了方便 C++11 用户自己手动实现了一个。实际也不复杂,主要是变长参数模板的处理,以及 std::forward 实现的完美参数转发。相比 std::move, std::forward 会进行引用折叠。
  2. 区分左值与右值。

3. 在传值的时候仍然应该使用 * 以及 &

这是最安全而且性能最好的。
不要使用智能指针作参数,无论是 by value 还是 by reference,除非你想在函数里控制对象的生命周期。
无论是 copy 还是 assign 一个 shared_ptr,都会影响引用计数,改变对象的生命周期。
比较理想的做法应该是传递引用或裸指针。
例如:

auto upw = make_unique<widget>(); 
...
f( *upw );

auto spw = make_shared<widget>();
...
g( spw.get() );

auto 关键字

主要讲两点。

  1. 关于性能
auto x = value;

这个语句是否创建了一个 value 然后通过 copy/move 的方式给 x 呢?
并没有。实际上以下两句是等同的。

T x = a;  // 1
T x(a);  // 2

那么形如:

auto x = type{value};

这个语句是否创建了一个临时对象并且通过 copy/move 转移给 x 呢?
答案是肯定的,但是编译器可能会优化。并且仍然需要保证这个临时对象是 copyable/movable 的。

  1. 关于不能使用 auto 的地方
    在使用上面第二种方式初始化时,auto 不适用于无法 copy/move 或者 copy/move 代价昂贵的对象。
auto lg = lock_guard<mutex>{mu};  // error, not movable
auto ai = atomic<int>{0};  // error, not movable
auto a = array<int, 50>{};  // compiles, but needlessly expensive

4. 右值优化

在参数传递中,可以恰当地使用右值优化。

#include <string>
#include <iostream>

class Employee {
public:
  // 1
  void set_name(const std::string& name) {
    name_ = name;
    std::cout << "set name with lvalue" << std::endl;
  }
  // 2
  void set_name(std::string&& name) noexcept {
    name_ = std::move(name);
    std::cout << "set name with rvalue" << std::endl;
  }
  void speak() {std::cout<< "My name is " << name_ << std::endl;}
private:
  std::string name_;
};

int main(int argc, char const *argv[]) {
  Employee e;
  std::string s = "ssss";
  std::string b = "bbbb";
  e.set_name(s);
  e.speak();
  e.set_name(s+b);
  e.speak();
  return 0;
}

值得注意的是 noexcept 这个关键字。由于函数 1 有可能会发生内存分配(例如传递一个右值作为参数),因此是可能发生诸如内存不足等异常的。而函数 2 是不可能的。这种设计体现了 exception-safety。

但是,对于构造函数,建议使用传值的方式。原因可以参考 stackoverflow。例子如下,注意是使用了 std::move。

class Employee {
public:
  Employee() {}
  //  for constructor, pass by value is a good idea
  explicit Employee(std::string name): name_(std::move(name)) {
    
  }
private:
  std::string name_;

5. 正确使用 &&

&& 不仅表示 rvalue reference,还可以表示 forwarding reference。
两者的使用场景不同。

  1. rvalue reference 用于右值优化,例如上面的:
  // 1
  void set_name(const std::string& name) {
    name_ = name;
    std::cout << "set name with lvalue" << std::endl;
  }
  // 2
  void set_name(std::string&& name) noexcept {
    name_ = std::move(name);
    std::cout << "set name with rvalue" << std::endl;
  }
  1. forwarding reference 用于编写 forwarder,可以在保留参数性质(左值、右值、const)的情况下传递参数。
namespace c14 {
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args) {
  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

6. 使用 tuple 返回多个值

这里介绍了三种方式,个人推荐第三种,更清晰一点。

#include <set>
#include <tuple>
#include <iostream>

using namespace std;
int main(int argc, char const *argv[]) {
  set<string> aSet{"Hello", "World", "SB"};

  string str1 = "Hello";
  string str2 = "You";
  string str3 = "Me";

  // C98
  pair<set<string>::iterator, bool> result1 = aSet.insert(str1);
  if (result1.second == true) {
    cout << "Inserted: " << *result1.first << endl;
  } else {
    cout << "Insert " << str1 << " failed." << endl;
  }

  // C11: auto
  auto result2 = aSet.insert(str2);
  if (result2.second == true) {
    cout << "Inserted: " << *result2.first << endl;
  }

  // C11: tie
  set<string>::iterator iter;
  bool success;
  tie(iter, success) = aSet.insert(str3);
  if (success == true) {
    cout << "Inserted: " << *iter << endl;
  }
  return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容