Search code examples
c++c++11overloadingsemanticslanguage-lawyer

What's the exact semantics of deleted member functions in C++11?


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?


Solution

  • 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.