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

AI Thread Summary
The discussion centers on the use of the "= delete" syntax in C++, particularly in the context of the thread library. Participants clarify that "delete" is a C++ feature introduced in 2011 that prevents the compiler from generating default assignment operators and copy constructors for classes like "thread," which do not make sense for threading operations. The line of code in question, "thread & operator=(const thread &)", is identified as the assignment operator for the thread class, but the "= delete" declaration indicates that this function is intentionally not available. This distinction is crucial as it helps prevent misuse of thread objects, ensuring that they cannot be copied or assigned, which aligns with the design principles of threading in C++. The conversation emphasizes the complexity of operator overloading and the importance of understanding these features for effective C++ programming.
NotASmurf
Messages
150
Reaction score
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
It may be typing the variable as in
Java:
float y = 3.0;

int x = (int ) y;
 
What programming language is it?
 
it's c++
 
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.
 
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
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
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
Back
Top