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;
相关推荐
感哥3 小时前
C++ 多态
c++
沐怡旸10 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River41613 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥16 小时前
C++ std::set
c++
侃侃_天下17 小时前
最终的信号类
开发语言·c++·算法
博笙困了17 小时前
AcWing学习——差分
c++·算法
青草地溪水旁17 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁17 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥18 小时前
C++ std::vector
c++
zl_dfq18 小时前
C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
c++