Modern Effective C++ Item 11:优先考虑使用deleted函数而非使用未定义的私有声明

C++98 方法:private

C++98 将特殊成员函数(如拷贝构造函数和拷贝赋值运算符)声明为私有且不定义。这种方法可以防止客户端调用这些函数,但如果在成员函数或友元函数中调用这些函数,会在链接时引发错误。C++11 使用 = delete 将这些函数标记为删除的函数。删除的函数不能以任何方式被调用,即使在成员函数或友元函数中调用也会在编译时失败。这比 C++98 的方法更安全,因为错误在编译阶段就能捕获。删除的函数通常声明为 public 而不是 private。这是因为当客户端代码试图调用成员函数时,编译器会先检查访问性,再检查删除状态。如果函数是 private 的,编译器可能会只报告访问性错误,而不会提到函数已被删除。

cpp 复制代码
template <class charT, class traits = char_traits<charT> >
class basic_ios : public ios_base {
public:
    ...
private:
    basic_ios(const basic_ios& );           // not defined
    basic_ios& operator=(const basic_ios&); // not defined
};
C++11 方法 =delete

使用=delete可以禁止特定类型的函数调用,可以禁止特定类型的模板实例化,类内的模板函数特化,如果类内有一个模板函数,使用 = delete 可以在类外删除特定的模板实例。这是因为在类内不能给特化的成员模板函数指定不同的访问级别,而在类外删除这些函数不会有问题。

cpp 复制代码
template <class charT, class traits = char_traits<charT> >
class basic_ios : public ios_base {
public:
    basic_ios(const basic_ios& ) = delete;
    basic_ios& operator=(const basic_ios&) = delete;
};

禁止特定类型的函数调用

cpp 复制代码
bool isLucky(int number);       // 原始版本
bool isLucky(char) = delete;    // 拒绝 char
bool isLucky(bool) = delete;    // 拒绝 bool
bool isLucky(double) = delete;  // 拒绝 float 和 double

禁止特定类型的模板实例化

cpp 复制代码
template<typename T>
void processPointer(T* ptr);
template<>
void processPointer<void>(void*) = delete;
template<>
void processPointer<char>(char*) = delete;
template<>
void processPointer<const void>(const void*) = delete;
template<>
void processPointer<const char>(const char*) = delete;
template<>
void processPointer<const volatile void>(const volatile void*) = delete;
template<>
void processPointer<const volatile char>(const volatile char*) = delete;
template<>
void processPointer<wchar_t>(wchar_t*) = delete;
template<>
void processPointer<char16_t>(char16_t*) = delete;
template<>
void processPointer<char32_t>(char32_t*) = delete;

类内的模板函数特化

cpp 复制代码
class Widget {
public:
    template<typename T>
    void processPointer(T* ptr){}
};
template<>
void Widget::processPointer<void>(void*) = delete;
相关推荐
随意02310 分钟前
STL 6分配器
开发语言·c++
jndingxin1 小时前
c++ 面试题(1)-----深度优先搜索(DFS)实现
c++·算法·深度优先
Watink Cpper1 小时前
[灵感源于算法] 算法问题的优雅解法
linux·开发语言·数据结构·c++·算法·leetcode
老一岁1 小时前
C++ 类与对象的基本概念和使用
java·开发语言·c++
随意0231 小时前
STL 3算法
开发语言·c++·算法
偷懒下载原神1 小时前
《C++ 继承》
开发语言·c++
随意0231 小时前
STL 4函数对象
开发语言·c++
温宇飞2 小时前
C++ 成员指针详解
c++
老猿讲编程2 小时前
汽车车载软件平台化项目规模颗粒度选择的一些探讨
c++·汽车
clock的时钟3 小时前
c++第七天--继承与派生
开发语言·c++