Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some uses for =delete? [duplicate]

Earlier today I asked a question that led to another one: When should I use =delete? I don't think there is a post dedicated solely to =delete on SO, so I looked it up in a book called "The C++ Programming Language". I will list my findings in my answer below.

Please comment or answer if there's more to say or if I'm mistaken.

like image 650
Oleksiy Avatar asked Sep 17 '13 05:09

Oleksiy


People also ask

What is the use of remove duplicates?

When you use the Remove Duplicates feature, the duplicate data will be permanently deleted. Before you delete the duplicates, it's a good idea to copy the original data to another worksheet so you don't accidentally lose any information. Select the range of cells that has duplicate values you want to remove.

Why is removing duplicates in data important?

This, in turn, leads to organisations holding more than one record of someone – possibly with conflicting information. Identifying and removing or merging these duplicate records from your database is a key part of forming an effective Single Customer View (SCV).

What do you do with duplicate data?

In the case of having duplicate data in a database, you can simply merge duplicate data as a single value, this will eliminate all duplicates from the database.

Should you remove duplicates?

You should probably remove them. Duplicates are an extreme case of nonrandom sampling, and they bias your fitted model. Including them will essentially lead to the model overfitting this subset of points.


1 Answers

It turns out that =delete is extremely useful! Here are a few examples:


Basically we can prevent copying base classes because it might often lead to slicing:

struct Base {

    Base(){}

    Base& operator=(const Base&) = delete; // disallow copying
    Base(const Base&) = delete;

    Base& operator=(Base && ) = delete;      // disallow moving
    Base(Base && ) = delete;

};

struct Der : public Base {};

void func() {

    Der d;
    Base base = d; // this won't work because the copy constructor is deleted!
                   // this behavior is desired - otherwise slicing would occur

}

It's also useful when a template function cannot run with a certain type:

template<class T>
void fn(T p) { /* ... */ }; // do something with T

void fn(int) = delete; // disallow use with int

void fun() {

    fn(4);      // aha! cannot use fn() with int!
    fn(-4.5);   // fine
    fn("hello");// fine
}

=delete can also disallow undesired conversions:

struct Z {

    Z(double); // can initialize with a double
    Z(int) = delete; // but not with an integer

};

void f() {

    Z z1 { 1 }; // error! can't use int
    Z z2 { 1.0 }; // double is ok

}

Some more advanced uses of =delete include prohibiting stack or free store allocation:

class FS_Only {
    ~FS_Only() = delete;  // disallow stack allocation
};

class Stack_Only {
    void* operator new(size_t) = delete;   // disallow heap allocation
};

... You get the idea. Hope this helps someone! =delete can help write readable, bugless and elegant code.


Edit:

As it was correctly pointed out in the comments, it is now impossible to delete FS_Only objects, so this one isn't such a good use of =delete after all.

like image 90
Oleksiy Avatar answered Sep 21 '22 09:09

Oleksiy