🐭 不可拷贝类的设计

我们知道有些对象 (比如文件流) 的拷贝与移动是没有意义的,那我们应该怎样禁止该类对象的拷贝和移动操作呢?有两种途径:

  1. 私有化声明拷贝和移动类成员函数
  2. 公开化删除拷贝和移动类成员函数 (C++11)

代码示例 :

  1. #include <iostream>
  2. class Noncopyable {
  3. protected:
  4. #if __cplusplus >= 201103L
  5. Noncopyable() = default;
  6. ~Noncopyable() = default;
  7. #else
  8. Noncopyable() {}
  9. ~Noncopyable() {}
  10. #endif
  11. #if __cplusplus >= 201103L
  12. Noncopyable(const Noncopyable &) = delete;
  13. Noncopyable &operator=(const Noncopyable &) = delete;
  14. #else
  15. private:
  16. Noncopyable(const Noncopyable &);
  17. Noncopyable &operator=(const Noncopyable &);
  18. #endif
  19. };
  20. class Job : Noncopyable {};
  21. int main(void) {
  22. Job j11 = Job{}; // works, C++17 copy elision
  23. // Job j21 = j11; // calls to delete copy ctor, error compile
  24. Job j22;
  25. // j22 = Job{}; // calls to delete copy assignment, error compile
  26. return 0;
  27. }

:::tips 公开化删除拷贝和移动类成员函数:可以直接放到 public 域下更好的展现出来,并且编译错误信息友好
私有化声明拷贝和移动类成员函数:只有声明没有定义,编译错误会延迟到链接阶段,并且提示信息不友好

这里的 Noncopyable 基类修改自 Boost 库的 noncopyable 类,protected 的使用,使其只能作为基类使用 :::

🐂 普通函数删除

已经公开了的函数,别人可能在使用,冒然删除,不够友好,实际上我们可以先给出废弃警告,给别人一些缓冲时间来做修改,然后在一个未来的版本中彻底删除。
代码示例 :

  1. // deletefn.cpp
  2. #include <iostream>
  3. // deprecated a function
  4. [[deprecated("use GoodLuckyExt instead")]] void GoodLucky() {
  5. std::cout << "GoodLucky\n";
  6. }
  7. void GoodLuckyExt() { std::cout << "GoodLuckyExt\n"; }
  8. // char, bool, double can implicitly convert to int
  9. // delete functions
  10. bool isLucky(int) { return true; }; // original function
  11. bool isLucky(char) = delete; // reject chars
  12. bool isLucky(bool) = delete; // reject bools
  13. bool isLucky(double) = delete; // reject doubles and floats
  14. int main(void) {
  15. // deprecated warning
  16. GoodLucky();
  17. // function deleted
  18. isLucky(2); // works
  19. // isLucky(2.0); // not compiles, calls to delete function
  20. // isLucky(true); // not compiles, calls to delete function
  21. return 0;
  22. }

:::warning $ clang++ deletefn.cpp -std=c++17
deletefn.cpp:19:3: warning: ‘GoodLucky’ is deprecated: use GoodLuckyExt instead [-Wdeprecated-declarations] :::

🐯 模板函数删除

模板函数的删除和普通函数的删除类似,实际上就是删除一些特化,偏特化的函数版本,这个技术也比较有用,能减少模板函数的实例化数量。
代码示例 :

  1. #include <iostream>
  2. // function template
  3. template <typename T> void processPointer(T *ptr) { std::cout << ptr << '\n'; }
  4. // delete specialized instantiations
  5. template <> void processPointer(void *ptr) = delete;
  6. template <> void processPointer(char *ptr) = delete;
  7. // function template in class
  8. struct Job {
  9. template <typename T> void processPointer(T *ptr) { std::cout << ptr << '\n'; }
  10. };
  11. // delete specialized instantiations outside of class, still as public
  12. template <> void Job::processPointer(void *ptr) = delete;
  13. template <> void Job::processPointer(char *ptr) = delete;
  14. int main(void) {
  15. int i{0};
  16. char c{'c'};
  17. processPointer(&i); // 0x7ffee8be4c08
  18. // processPointer(&c); // call to delete function
  19. Job j;
  20. j.processPointer(&i); // 0x7ffee8be4c08
  21. // j.processPointer(&c); // call to delete member function
  22. return 0;
  23. }

:::tips 私有化对类的模板成员函数无能为力,只能在类外使用 delete 删除指定的实例化版本 :::

🐰 总结

  1. delete 能删除类的特殊成员函数,也能删除类的模板成员函数
  2. delete 能删除自由函数,也能删除自由模板函数
  3. 删除一个公开的函数之前,可以给出废弃警告,得益于现代 C++ 的 [[deprecated]] 属性