Would someone mind explaining the meaning? Is it some sort of lifetime opt-in thing? Is it restricting how a method can be called? What's going on here? What is `=delete` telling the compiler?
Would someone mind explaining the meaning? Is it some sort of lifetime opt-in thing? Is it restricting how a method can be called? What's going on here? What is `=delete` telling the compiler?
This can allow for instance to explicitely erase overloads and "disable" functions or methods. For instance imagine that you don't want a function to be called with a float, just with a double, because having floats creeping in somewhere would be a bug and you want to catch as many bugs as possible: then you can just do the following.
void foo(float) = delete;
void foo(double) { }
int main()
{
// won't compile:
foo(1.23f);
// will compile:
foo(1.23);
}
without the following line: void foo(float) = delete;
1.23f would be implicitely converted to a double instead of causing a compile error.One case where I found it super useful is for instance to disable constructors taking a pointer when you have ctors taking a bool:
struct foo {
template<typename T>
foo(T*) = delete;
foo(bool);
};
int main()
{
void* x = nullptr;
// won't compile: foo f(x);
// won't compile: foo g("blablaba");
}
this (rightly) causes a compile error on both the creation of f and g, while if foo(T*) = delete; was not there then conversions from ptr to bool would happen: struct foo {
foo(bool);
};
int main()
{
void* x = nullptr;
foo f(x); // compiles :'(
foo g("blablaba"); // compiles :'(
}In several cases the C++ compiler will generate default implementation for specific methods.
However there are cases where that isn't desirable, and until =delete came to be, the workaround was to mark the method as private without implementation.
So =delete deals away with such hack, although as mentioned this is a superficial overview of its purpose.
When they want to introduce a new concept (in this case, a way to express that using an implicit function or overload is forbidden) they use one of the already-reserved keywords ("delete"). This sometimes leads to a clunky reading experience, but is better than breaking anyone's code that happens to use a variable or function named "forbidden" or similar.
C++ has a lot of nice ideas under the hood, but the syntax and reading experience definitely isn't ideal because of the desire for backwards compatibility (which includes the very strong desire to not introduce new keywords / tokens if there is any possible way to avoid it)
The example they give is the reference wrapper, which (as the name might suggest), is used to wrap a reference to some thing. To reiterate, with hopefully a bit more of what is going on.
If the creator of this method had just created a function:
template<class T> auto cref(const T&) -> std::reference_wrapper<const T>;
Then by C++ overloading rules, you could call this function with either an const ref LValue, or an RValue of type T. (In their example, they used a number). Which for 95% of functions is totally fine, and desired behavior! However, in this case, it would be bad, because remember, we are trying to wrap a reference to some value (e.g. basically a pointer under the hood), so a pointer to some ephemeral value is likely not what we want.
In order to solve this, the creator explicitly adds: template<class T> auto cref(const T&&) = delete;
Now, if the user calls cref on an RValue like a number, they will get a nice error message saying that the function has been explicitly deleted. The reason this works is because "const T&&" is the better match for the type of an RValue, compared to "const T&", so this is the overload that the compiler will try to use, rather than the original version above.
Some other reasons to use delete, is if you want to do stuff like explicitly forbid the use of copy constructors, because you have some object that you don't want copies made of for whatever reason. By explicitly deleting the copy constructor, you can be sure that no copies are accidentally made somewhere in user code (which is especially useful when your code interacts with other libraries you don't control)
I hope that made sense, but the TLDR is that there are a lot of ways construct objects, delete objects, to copy / move data around, etc. If you are doing low-level memory management / optimization, you may want to explicitly tell the compiler not to do the normal "helpful" stuff it does for you, such as creating default constructors / overloads, and this is the supported way of doing that in the standard.