C/++/# Explain this line "thread& operator = (const thread&) = delete;"

  • Thread starter NotASmurf
  • Start date
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?
 
10,686
4,243
It may be typing the variable as in
Java:
float y = 3.0;

int x = (int ) y;
 
10,686
4,243
What programming language is it?
 
it's c++
 

pasmith

Homework Helper
1,733
408
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?
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.
 

Filip Larsen

Gold Member
1,212
149
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.
 
1,497
607
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.
 

Filip Larsen

Gold Member
1,212
149
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.
 

Want to reply to this thread?

"Explain this line "thread& operator = (const thread&) = delete;"" You must log in or register to reply here.

Related Threads for: Explain this line "thread& operator = (const thread&) = delete;"

Replies
2
Views
21K
  • Posted
Replies
3
Views
2K
Replies
3
Views
1K
  • Posted
Replies
6
Views
2K
S
  • Posted
Replies
12
Views
771

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top