Explain this line "thread& operator = (const thread&) = delete;"

In summary: The delete operator is a standard C++ operator that destroys objects. A deleted function, on the other hand, is a function that is no longer in use, but its definition remains in the source code.
  • #1
NotASmurf
150
2
here delete, thread, and const are obviously keywords, I saw this when the system libraries threw a breakpoint, not my code, I'm new at this and that line of code makes very little sense to me, afaik, (const thread&) should be a conversion operator, how can you set an operator to a statement? then set a variable to that?
 
Technology news on Phys.org
  • #2
It may be typing the variable as in
Java:
float y = 3.0;

int x = (int ) y;
 
  • #3
What programming language is it?
 
  • #4
it's c++
 
  • #5
NotASmurf said:
here delete, thread, and const are obviously keywords, I saw this when the system libraries threw a breakpoint, not my code, I'm new at this and that line of code makes very little sense to me, afaik, (const thread&) should be a conversion operator, how can you set an operator to a statement? then set a variable to that?

NotASmurf said:
it's c++

Without the "= delete" this would be a function declaration, overloading the assignment operator for a class called "thread" (it's odd that the class name isn't capitalized).

The "= delete;" part is apparently a new (ie. only introduced in 2011) C++ language feature which should cause the compiler to quit with a "this function is deleted" error: see various answers to this post on stack exchange.
 
  • #6
The line looks like its from the standard thread library and it has the effect (as mentioned by others) that any default assignment operator that the compiler otherwise will add automatically are deleted (i.e. made not available). In this particular case the thread library uses delete (also on the copy constructor) because otherwise the compiler will add a default copy constructor and copy operator which makes no sense for threads. Note that the corresponding move constructor and assignment operator is defined.
 
  • Like
Likes NotASmurf
  • #7
C++ can look pretty strange when it comes to operator overloading and function pointers.

The first part: thread & operator = (const thread &) refers to the assignment operator for a thread. It is what will be called if you do something like this.

Code:
thread t1;
thread t2;
t1 = t2;  //The assignment operator is called here

The second part is assigning this behavior to be the same as the "delete" operator for the thread class.
 
  • Like
Likes NotASmurf
  • #8
newjerseyrunner said:
The second part is assigning this behavior to be the same as the "delete" operator for the thread class.

That is not correct. The "=delete" part declares the method as a deleted function, as mentioned several times already in this thread. Deleted functions are not to be confused with the delete operator.
 
  • Like
Likes newjerseyrunner

FAQ: Explain this line "thread& operator = (const thread&) = delete;"

What does "thread& operator = (const thread&) = delete;" mean in C++?

This line is a declaration of a function called "operator=" that is used for assigning one thread object to another. The "= delete" part indicates that this function is intentionally disabled and cannot be used.

Why would this function be disabled?

This function is disabled because it is not safe to assign one thread object to another. Threads represent different execution paths and assigning them can lead to unpredictable behavior and errors in the program.

What is the purpose of using "thread&" in this line?

The "thread&" part indicates that this function returns a reference to a thread object. This allows the function to be used in expressions and to modify the thread object that is being assigned to.

What is the difference between "const thread&" and "thread&"?

The "const" keyword indicates that the thread object being passed as a parameter cannot be modified within the function. This is used when the function does not need to make changes to the thread object. In contrast, "thread&" allows the function to modify the thread object.

Can this function be overridden or redefined?

No, this function cannot be overridden or redefined because it has been explicitly disabled using the "= delete" notation. This means that the compiler will not allow any attempts to use or redefine this function.

Back
Top