Article 11 of "Effective Modern C++" study notes: The delete function is preferred instead of the private undefined function

If you implement a certain class for other programmers to use, but don't want him to use a specific function, then we can simply not declare the function. But if this function is automatically generated by the compiler, such as the default assignment function, what should we do?

The practice in C++98 is to declare the function as private, so that it prevents the outside from directly calling it, and then we don’t define it, so that when a member function of this class calls the function by mistake, a link error will be prompted . E.g:

class Test {
public:
    void Fix(Test& t) {
        Test t1;
        t1 = t;  //编译报错,使用未定义函数
    }
private:
    Test& operator=(const Test& t);//只声明,不定义
};

Test t1,t2;
t1 = t2;   //编译报错,private权限问题

But C++11 added a new keyword for us, which can easily solve this problem, that is, the delete keyword. For the same code as above, you only need to add delete after the assignment function.

class Test {
public:
    void Fix(Test& t) {
        Test t1;
        t1 = t;  //编译报错,使用delete函数
    }

    Test& operator=(const Test& t) = delete;//只声明,不定义
};

Test t1,t2;
t1 = t2;   //编译报错,使用delete函数

Another advantage of using delete is that delete can not only modify member functions, but also ordinary global functions can use delete, especially in the case of function overloading, delete is still very useful, because even though this function is deleted, it It is still a part of the program, and it will still be taken into account when reloading the resolution. In particular, when the delete function is finally matched, a compilation error will be prompted.

//无delete场景
bool isLove(int);

isLove(12); //匹配到isLove(int)
isLove(false);//false隐式转换为int,匹配到isLove(int)
isLove(3.5);//double隐式转换为int,匹配到isLove(int)


//使用delete场景
bool isLove(int);
bool isLove(bool) = delete;
bool isLove(double) = delete;

isLove(12); //匹配到isLove(int)
isLove(false);//匹配到isLove(bool),因其为删除函数,编译报错
isLove(3.5); //匹配到isLove(double),因其为删除函数,编译报错

In the following scenario, you will have to use delete, that is, to delete a certain type of realization function of a certain template function (member function or global function). This can only be done with the help of the delete keyword:

//全局函数
template<class T>
void TestFunc(T* ptr);

template<>
void TestFunc<void>(void*) = delete;   //删除该模板的void类型具现

template<>
void TestFunc<char>(char*) = delete;   //删除该模板的char类型具现


//成员函数,类内类外均可删除
class TestClass{
public:
    template<class T>
    void TestFunc2(T* ptr);
    
    template<>
    void TestFunc2<void>(void*) = delete;   //类内删除该模板的void类型具现
};

template<>
void TestClass::TestFunc2<char>(char*) = delete;   //类外删除该模板的char类型具现

Shorthand

  • The delete function is preferred instead of the private undefined function
  • Any function can be deleted, including member functions, non-member functions, and template realization

 

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/114218440