【C++】Core Guideline总结

REF:

资料:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-consume

其他人的笔记(很多有用的笔记):

https://www.jianshu.com/p/e4f4a34da6ba

知识储备:

https://blog.csdn.net/kongkongkkk/article/details/77414410 (POD相关,仅概念)

了解vector扩容问题请戳传送门——vector扩容问题源代码剖析

详细POD定义说明请戳传送门——POD数据类型解释说明

详细is_pod函数定义说明请戳传送门——is_pod函数定义说明

 

POD,是Plain Old Data的缩写,普通旧数据类型,是C++中的一种数据类型概念*

 

前言

C++ 是永远也学不完的语言,最近发现了一个不错的教程 C++ Core Guidelines,希望记录下自己阅读时的心得。
本文主要是为了记录自己的知识盲区,可能不适用于其他读者。

一致地定义 copy, move and destroy

如果你需要自定义 copy/move constructor, copy/move assignment operator, destructor 这 5 个函数,说明你期望做一些默认行为之外的事,因此需要保持以下的一致性:

  • 定义了拷贝构造函数,就要定义拷贝赋值函数

  • 定义了移动构造函数,就要定义移动赋值函数

    X x1;
    X x2 = x1; // ok
    x2 = x1;   // pitfall: either fails to compile, or does something suspicious
    
  • 如果一个类的基类或者成员变量中有任何一个定义了移动构造函数,则该类也应该定义移动构造函数。

  • 自定义了析构函数,同时也要定义或者禁用 copy/move

 

class AbstractBase {
public:
  virtual ~AbstractBase() = default;
  AbstractBase(const AbstractBase&) = default;
  AbstractBase& operator=(const AbstractBase&) = default;
  AbstractBase(AbstractBase&&) = default;
  AbstractBase& operator=(AbstractBase&&) = default;
};

合理使用 noexcept

将一个函数定义为 noexcept 有助于编译器生成更有效率的 code。因为不需要记录 exception handler。但是 noexcept 并非随意使用的。
主要用于不会抛出异常的函数,例如纯 C 写成的函数,也可以是一些非常简单的例如 setter 和 getter 函数。对于可能抛出内存不足等无法解决的异常的函数,也可以使用。

 

vector<string> collect(istream& is) noexcept
{
    vector<string> res;
    for (string s; is >> s;)
        res.push_back(s);
    return res;
}

非常适合于使用 low-level 接口写的频繁使用的函数。
值得注意的是,以下几种函数不允许抛出异常:

  • 析构函数
  • swap 函数
  • move 操作
  • 默认构造函数

仅在需要操作对象生命周期时使用智能指针作参数

(见过不少炫技代码强行使用智能指针)
最佳方式还是使用引用 T& 作为函数参数。使用 T* 不明确所有权,并且需要检查空指针。使用unique_ptr<T>限制了调用者也必须使用智能指针。使用shared_ptr<T>会导致性能上的损失(引用计数的原子操作)。

 

// accepts any int*
void f(int*);

// can only accept ints for which you want to transfer ownership
void g(unique_ptr<int>);

// can only accept ints for which you are willing to share ownership
void g(shared_ptr<int>);

// doesn't change ownership, but requires a particular ownership of the caller
void h(const unique_ptr<int>&);

// accepts any int
void h(int&);

使用 T&&std::forward 转发参数

如果参数被传递进某个函数中,但是并不直接在这个函数中使用,则使用T&& 传递,并只进行std::forward 操作来实现“完美转发”。参数是不是 const, 是左值还是右值都会被保留,完美传递给下一层函数。

 

template <class F, class... Args>
inline auto invoke(F f, Args&&... args) {
    return f(forward<Args>(args)...);
}

返回 T& 当你不期望拷贝或者返回空

例如:

 

class Car
{
    array<wheel, 4> w;
    // ...
public:
    wheel& get_wheel(int i) { Expects(i < w.size()); return w[i]; }
    // ...
};

void use()
{
    Car c;
    wheel& w0 = c.get_wheel(0); // w0 has the same lifetime as c
}

错误的例子:

 

int& f()
{
    int x = 7;
    // ...
    return x;  // Bad: returns reference to object that is about to be destroyed
}

lambda

[captures] (params) -> ret {body}
捕获变量:

  • = 表示拷贝 (而非 const 引用),强调 correctness。是按值传入的,但是变量在 lambda 内是 const 的。值得注意的是,= 捕获的变量在 lambda 构造的时候就确定了,而非调用的时候确定。
  • & 表示引用,强调 efficiency。并没有 const 引用的捕获方式。

假设 message 是一个较大的网络消息,拷贝比较昂贵。可以捕获单个变量:

 

std::for_each(begin(sockets), end(sockets), [&message](auto& socket)
{
    socket.send(message);
});

值得注意的是,使用 [=] 在类内捕获变量时,会捕获 this,导致出现不期望的结果(修改了某个类成员变量会影响按值传递的 lambda 的行为)。在 c++20 标准中,[=]已经不再捕获 this

 

class My_class {
    int x = 0;
    // ...

    void f() {
        int i = 0;
        // ...

        auto lambda = [=]{ use(i, x); };   // BAD: "looks like" copy/value capture
        // [&] has identical semantics and copies the this pointer under the current rules
        // [=,this] and [&,this] are not much better, and confusing

        x = 42;
        lambda(); // calls use(0, 42);
        x = 43;
        lambda(); // calls use(0, 43);

        // ...

        auto lambda2 = [i, this]{ use(i, x); }; // ok, most explicit and least confusing

        // ...
    }
};

什么时候使用struct,什么时候使用class

使用 class 说明存在 invariant (即存在一些逻辑约束,不能任意更改其值,如果所有的类成员独立,则不存在 invariant)。使用 struct 说明允许独立更改每一个数据成员。例如:

 

struct Pair {
    string name;
    int volume;
};

class Date {
public:
    // validate that {yy, mm, dd} is a valid date and initialize
    Date(int yy, Month mm, char dd);
    // ...
private:
    int y;
    Month m;
    char d;    // day
}

简单来讲,如果你定义了任意一个类成员为 private,则应该用 class

按照成员变量的顺序初始化

构造时的初始化顺序是按照成员变量的顺序来的。如果不按照该顺序,则会导致非预期的行为。

 

#include <iostream>
class A {
private:
  int num1;
  int num2;
public:
  explicit A(int n): num2(n), num1(++n) {
    // expect 11, 10 but get 11, 11
    std::cout << num1 << ", "<< num2 << std::endl;
  }
};

int main(int argc, char const *argv[]) {
  A a(10);
  return 0;
}

基类应该禁止拷贝,但是提供一个 clone() 虚函数

这是为了防止对象被“截断“。因为一个普通的拷贝操作只会拷贝基类成员。对于一个有虚函数的类(会被继承),不应该有拷贝构造函数和拷贝赋值函数。

 

class B { // GOOD: base class suppresses copying
public:
    B(const B&) = delete;
    B& operator=(const B&) = delete;
    virtual unique_ptr<B> clone() { return /* B object */; }
    // ...
};

class D : public B {
    string more_data; // add a data member
    unique_ptr<B> clone() override { return /* D object */; }
    // ...
};

auto d = make_unique<D>();
auto b = d.clone(); // ok, deep clone

这里需要注意的是,无论在基类还是派生类中,clone() 返回的都是基类的智能指针 unique_ptr<B>

使用工厂模式来定制初始化时的 "virtual behavior"

不要在构造函数中调用虚函数。

 

#include <iostream>

class Base {
public:
  Base() noexcept {
    init();
  }
  virtual ~Base() {
    std::cout << "base deleted" << std::endl;
  }

  virtual void init() {
    std::cout << "init base" << std::endl;
  }
};

class Derived: public Base {
public:
  ~Derived() {
    std::cout << "derived deleted" << std::endl;
  }

  virtual void init() override {
    std::cout << "init derived" <<std::endl;
  }
};

int main(int argc, char const *argv[]) {
  Base* a = new Derived();
  a->init();
  delete a;
  return 0;
}

以上程序的意图是想通过派生类不同的 init() 实现来进行不同的初始化。然而这并不能如预期实现。输出结果是:

 

init base
init derived
derived deleted
base deleted

显然,构造的时候用的仍然是基类的 init()
从概念上讲,因为在构造派生类前会先构造基类,此时派生类的实例还没构造完成,从cpp语言层面来讲不允许去操作初始化的成员。
从实现上讲,在构造实例时我们会设置虚指针 vptr,该指针会随着类继承的顺序改变指向,如果有个更晚的派生类被构造,则会指向该类的虚表vtable,如此直到实例构造结束,所以 vptr 的指向是由最后调用的构造函数确定的。因此,在构造到基类时,只会指向基类的虚表。
为了解决这个问题,我们一般使用工厂函数。
工厂函数一般返回 unique_ptr

 

#include <iostream>
#include <memory>

class Base {
protected:
  Base() {} // avoid directly invoking

public:
  virtual void init() {std::cout << "base init" << std::endl;}

  virtual ~Base() {std::cout << "base deleted" << std::endl;}

  template<typename T, typename... Args>
  static std::unique_ptr<T> Create(Args &&... args) {
    auto p = std::make_unique<T>(std::forward<Args>(args)...);
    p->init();
    return p;
  }
};

class Derived : public Base {
public:
  ~Derived() {std::cout << "derived deleted" << std::endl;}
  virtual void init() override {std::cout << "derived init" << std::endl;}
};

int main(int argc, char const *argv[]) {
  auto p = Base::Create<Derived>();
  p->init();
  return 0;
}

注意这里将基类的构造函数设置为 protected 避免被误用于构造。
通过 Create() 方法可以方便地构造实例。输出结果为:

 

derived init
derived init
derived deleted
base deleted

委托构造函数 (delegating constructors)

委托构造函数是 c11 引入的新特性,可以在一个构造函数中调用另一个构造函数。这样我们就能够避免维护重复的构造函数代码。
例如,如果不使用该特性,我们需要在每个构造函数中检查参数。

 

class Date {   // BAD: repetitive
    int d;
    Month m;
    int y;
public:
    Date(int ii, Month mm, year yy)
        :i{ii}, m{mm}, y{yy}
        { if (!valid(i, m, y)) throw Bad_date{}; }

    Date(int ii, Month mm)
        :i{ii}, m{mm} y{current_year()}
        { if (!valid(i, m, y)) throw Bad_date{}; }
    // ...
};

其语法如下,注意使用大括号。

 

class Date2 {
    int d;
    Month m;
    int y;
public:
    Date2(int ii, Month mm, year yy)
        :i{ii}, m{mm}, y{yy}
        { if (!valid(i, m, y)) throw Bad_date{}; }

    Date2(int ii, Month mm)
        :Date2{ii, mm, current_year()} {}
    // ...
};

继承构造函数 (inheriting constructors)

有时候我们需要为一个类扩展一些方法,但是不改变其构造。这时候我们如果使用继承,则需要对基类的每个构造函数都重复以下代码:

 

#include <iostream>

class Base {
public:
  explicit Base(int a): a_(a) {}
protected:
  int a_;
};

class Derived: public Base {
public:
  explicit Derived(int a): Base(a) {} // repeat this for all constructors
  // methods
  void describe() {
    std::cout << a_ << std::endl;
  }
};

如果使用 using 关键字实现继承构造函数,则会简单的多:

 

#include <iostream>

class Base {
public:
  explicit Base(int a): a_(a) {}
protected:
  int a_;
};

class Derived: public Base {
public:
  using Base::Base; // inherit from base class
  // methods
  void describe() {
    std::cout << a_ << std::endl;
  }
};

int main(int argc, char const *argv[]) {
  Derived d(10);
  d.describe();
  return 0;
}

copy assignment 和 move assignment

copy assignment 和 move assignment 在处理 self assignment 的时候会有区别。因为将自己 move 到自己可能导致内存错误。在 copy assignment 中我们为了效率可以直接 copy 不做这个check, 而在 move assignment 中必须做。

 

class Foo {
    string s;
    int i;
public:
    Foo& operator=(const Foo& a);
    Foo& operator=(Foo&& a);
    // ...
};

Foo& Foo::operator=(const Foo& a)
{
    s = a.s;
    i = a.i;
    return *this;
}

Foo& Foo::operator=(Foo&& a) noexcept  // OK, but there is a cost
{
    if (this == &a) return *this;
    s = std::move(a.s);
    i = a.i;
    return *this;
}

不要为虚函数提供默认参数

override 的时候并不会覆盖原来的默认参数。这是比较好理解的,虚表中保存的是函数指针,跟参数无关。

 

#include <iostream>

class Base {
public:
  virtual int multiply(int val, int factor=2) = 0;
  virtual ~Base() {}
};

class Derived : public Base {
public:
  int multiply(int val, int factor=10) final {
    return val * factor;
  }
};

int main(int argc, char const *argv[]) {
  Derived d;
  Base& b = d;
  std::cout << b.multiply(10) << std::endl;  // 20
  std::cout << d.multiply(10) << std::endl;  // 100
  return 0;
}

使用指针或者引用访问多态对象

否则会导致得到的是“截断”至基类的对象。

 

// Access polymorphic objects through pointers and references
#include <iostream>

struct B {
  int a=0;
  virtual void func() {
    std::cout << "a = " << a << std::endl;
  }
};

struct D : public B {
  int b=1;
  void func() final {
    std::cout << "b = " << b << std::endl;
  }
};

void fault_use(B b) {
  b.func();
}

void correct_use(B& b) {
  b.func();
}

int main(int argc, char const *argv[]) {
  D d;
  fault_use(d); // a = 0
  correct_use(d); // b = 1
  return 0;
}

使用 enum class 替换 enum 和宏

enum 存在三个主要缺点:

  • 与整形之间的隐式转换
    可以比较两个不同枚举类型的大小。
  • 作用域
    在一个 enum 使用过的变量名不能用于另一个 enum
  • 不同编译器实现 enum 的底层数据类型不同
    使用 signed 还是 unsigned,使用 8bit,16bit 还是 32bit,不同编译器实现不一样。

宏的缺点:

  • 不遵从 scope 和类型的规则
  • 宏在编译后变量名就消失了,不利于 debug

此外,enum class 中的变量命名需要避免全部大写,与宏定义混淆。

 

#include <iostream>

enum class Color{
  red = 0xFF0000,
  green = 0x00FF00,
  blue = 0x0000FF
};

void print_color(Color c) {
  switch (c) {
    case Color::red:
      std::cout << "red" << std::endl;
      break;
    case Color::green:
      std::cout << "green" << std::endl;
      break;
    case Color::blue:
      std::cout << "blue" << std::endl;
      break;
    default:
      std::cout << "unknown" << std::endl;
  }
}


int main(int argc, char const *argv[]) {
  Color c = Color::blue;
  print_color(c);
  return 0;
}

使用 weak_ptr 避免循环引用

循环引用会导致无法释放内存。例如下面的代码,如果 ManWoman 都用 shared_ptr 相互引用,则会导致双方无法销毁。
由于不具备所有权,weak_ptr是不能直接使用的引用对象的,必须通过lock()方法生成一个 shared_ptr,暂时获取所有权再使用。

 

// use weak_ptr to break cycles of shared_ptr
#include <iostream>
#include <memory>

class Woman;

class Man {
public:
  void set_wife(const std::shared_ptr<Woman> &w) { wife_ = w; }

  void walk_the_dog() { std::cout << "man walks the dog" << std::endl; }

  ~Man() { std::cout << "Man destroyed" << std::endl; }

private:
  std::shared_ptr<Woman> wife_;
};

class Woman {
public:
  void set_husband(const std::weak_ptr<Man> &m) { husband_ = m; }

  void use_husband() {
    if (auto husband = husband_.lock()) {
      husband->walk_the_dog();
    }
  }

  ~Woman() { std::cout << "Woman destroyed" << std::endl; }

private:
  std::weak_ptr<Man> husband_;
};

int main(int argc, char const *argv[]) {
  auto m = std::make_shared<Man>();
  auto w = std::make_shared<Woman>();
  m->set_wife(w);
  w->set_husband(m);
  w->use_husband();
  return 0;
}

不要使用 C 风格的变长参数函数(variadic function)

不是类型安全的,而且需要复杂的语法和转换。推荐使用cpp模板和重载实现。

 

#include <iostream>

void print_error(int severity) {
  std::cerr << '\n';
  std::exit(severity);
}

template<typename T, typename... Ts>
constexpr void print_error(int severity, T head, Ts &&... tail) {
  std::cerr << head << " ";
  print_error(severity, std::forward<Ts>(tail)...);
}

int main(int argc, char const *argv[]) {
  print_error(1);  // Ok
  print_error(2, "hello", "world");  // Ok
  std::string s = "my";
  print_error(3, "hello", s, "world");  // Ok
  print_error(4, "hello", nullptr);  // compile error
  return 0;
}

使用 std::call_once 或者静态局部变量实现“初始化一次”

从 c11 开始,静态局部变量的实现就是线程安全的了。你不需要自己实现 double-checked locking 来初始化(通常你的实现都是错误的)。

 

// Do not write your own double-checked locking for initialization
#include <iostream>
class LargeObj {
public:
  LargeObj() { std::cout << "Large object initialized..." << std::endl; }
};

void func() {
  static LargeObj obj;
  static std::once_flag flg;
  std::call_once(flg, []() { std::cout << "call once..." << std::endl; });
  std::cout << "invoke func..." << std::endl;
}

int main(int argc, char const *argv[]) {
  func();
  func();
  func();
  return 0;
}

以上代码输出为:

 

Large object initialized...
call once
invoke func...
invoke func...
invoke func...

如果你头铁非要使用 double-checked locking,以下是可以保证线程安全的最佳实现。

 

mutex action_mutex;
atomic<bool> action_needed;

if (action_needed.load(memory_order_acquire)) {
    lock_guard<std::mutex> lock(action_mutex);
    if (action_needed.load(memory_order_relaxed)) {
        take_action();
        action_needed.store(false, memory_order_release);
    }
}

使用 using 替代 typedef

可读性。例如,同样定义一个函数指针类型:

 

typedef int (*PFI)(int);   // OK, but convoluted

using PFI2 = int (*)(int);   // OK, preferred

此外,using 还能用于模板类型:

 

template<typename T>
typedef int (*PFT)(T);      // error

template<typename T>
using PFT2 = int (*)(T);   // OK


 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值