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

Click For Summary

Discussion Overview

The discussion centers around the C++ code line "thread& operator = (const thread&) = delete;", exploring its meaning, implications, and the context in which it is used. Participants analyze the syntax and semantics of operator overloading and the use of the "delete" keyword in C++.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants express confusion about the line, questioning how an operator can be set to a statement and suggesting that "(const thread&)" should be a conversion operator.
  • Others provide examples of similar syntax in C++, comparing it to variable typing, such as "float y = 3.0;" and "int x = (int) y."
  • One participant identifies the programming language as C++ and notes the peculiarities of the syntax.
  • Another participant explains that without the "= delete," the line would declare an overloaded assignment operator for the "thread" class, which is not available due to the use of "delete." They mention that this feature was introduced in C++11.
  • Some participants clarify that the line is from the standard thread library, indicating that it prevents the compiler from generating a default copy constructor and assignment operator, which would be inappropriate for threads.
  • One participant elaborates on the assignment operator's role in the context of thread assignment, while another corrects a misunderstanding about the meaning of "= delete," emphasizing that it declares a function as deleted rather than assigning behavior.

Areas of Agreement / Disagreement

Participants generally agree on the function of the "= delete" syntax in preventing default operations for the thread class, but there is disagreement regarding the interpretation of the line and its implications, particularly around the nature of deleted functions versus the delete operator.

Contextual Notes

Some participants express uncertainty about the terminology and the implications of operator overloading in C++, indicating a need for clarification on these concepts.

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;
 
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   Reactions: 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   Reactions: 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   Reactions: newjerseyrunner

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
4
Views
3K
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K