a = A(); // error C2280
The expression on the right is a temporary which means it will look for operator=(A&&)
and sees it is deleted. Hence the error. There is no further search.
=delete
does not mean "don't use me, instead use next best one". It rather means, "don't use me when you need me — instead be alone in the wild."
Here is another example. If I want the instances of my class X
to be created with only long
and no other type (even if it converts into long!), then I would declare class X
as:
struct X
{
X(long arg); //ONLY long - NO int, short, char, double, etc!
template<typename T>
X(T) = delete;
};
X a(1); //error - 1 is int
X b(1L); //ok - 1L is long
That means, the overload resolution is performed before the compiler sees the =delete
part — and thus results in an error because the selected overload is found deleted.
Hope that helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…