What is the exact semantics of a remote function 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 & &)': attempt to reference a remote function

I'm just wondering why the compiler is not trying A& operator =(const A&); when A& operator =(A&&) deleted?

Is this behavior a defined C ++ standard?

+43
c ++ language-lawyer overloading semantics c ++ 11
Oct 09 '13 at 7:41
source share
2 answers
 a = A(); // error C2280 

The expression on the right is temporary, which means that it will look for operator=(A&&) and sees that it is deleted. Hence the error. There is no further search.

=delete means it doesn’t mean "don’t use me, but use the next best one." Rather, it means: "Do not use me when you need, instead of being alone in the wild."

Here is another example. If I want instances of my class X be created only with long , and of a different type (even if it converts to long!), 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 

This means that overload resolution is performed before the compiler sees =delete part - and therefore leads to an error because the selected overload is found deleted.

Hope this helps.

+67
Oct 09 '13 at 7:48
source share

When you =delete execute a function, you actually delete its definition.

8.4.3 Deleted Definitions [dcl.fct.def.delete]

1 Definition of a function of the form:

specifier attribute-seqopt decl-specifier-seqopt declarator = delete;

called remote definition. A function with a remote definition is also called a remote function.

But by doing so you also declare this function. Quoting from the standard [1] :

4 The remote function is implicitly built-in. [Note. A single definition rule (3.2) applies to remote definitions. -end note] Remote function definition should be the first function declaration [...]

And therefore, by making a = A() , the compiler actually resolves A::operator=(A&&) because it was declared (not A::operator(const A&) , because A&& is “more binding” to r-values ) However, when its definition is deleted, the line is poorly formed.

2 A program that refers to a remote function implicitly or explicitly, except for the declaration, is poorly formed.




[1] The tone of the underlined sentence is really necessary here. The standard states that a function declaration =delete d must first appear before other declarations. However, it supports the fact that deleting a function also declares a function.

+22
09 Oct '13 at 8:00
source share



All Articles