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;
相关推荐
咩咦1 小时前
C++学习笔记28:静态成员应用:不用循环求1到n的和
c++·学习笔记·类和对象·static·构造函数·oj·静态成员
EllinY1 小时前
CF2217E Definitely Larger 题解
c++·笔记·算法·构造
筠筠喵呜喵2 小时前
Linux软件开发性能优化
linux·c++·性能优化
Bruce_kaizy2 小时前
c++ linux环境编程——文件io介绍以及open 、write 、read 三剑客深度详解
linux·服务器·c++·ubuntu·操作系统·文件io
PAK向日葵5 小时前
我用 C++ 写了一个轻量级 Python 虚拟机,刚刚开源
c++·python·开源
玖釉-5 小时前
下一个排列:从字典序到原地算法的完整推导
数据结构·c++·windows·算法
枕星而眠5 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端
努力努力再努力wz5 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github
yunn_6 小时前
单例模式两种实现方法
开发语言·c++·单例模式
代钦塔拉9 小时前
C++ auto
开发语言·c++