Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

struct A
{
    A();

    A(const A&);
    A& operator =(const A&);

    A(A&&) = delete;
    A& operator =(A&&) = delete;
};

struct B
{
    B();

    B(const B&);
    B& operator =(const B&);    
};

int main()
{
    A a;
    a = A(); // error C2280

    B b;
    b = B(); // OK
}

My compiler is VC++ 2013 RC.

error C2280: 'A &A::operator =(A &&)' : attempting to reference a deleted function

I just wonder why the compiler doesn't try A& operator =(const A&); when A& operator =(A&&) is deleted?

Is this behavior defined by the C++ standard?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...