2017-10-14 11 views
6

이 이유 :명시 적으로 삭제 이동 생성자

error: use of deleted function ‘A::A(A&&)’ 

그리고는 이동 생성자를 추가 할 때

A(A&&) { 
    cout << "move constructor" << endl; 
} 

가 잘 컴파일,하지만 왜 programm에의의 :

struct A 
{ 
    A(int) { 
     cout << "construct from int" << endl; 
    } 

    A(A&&) = delete; 

    A(const A &) { 
     cout << "copy constructor" << endl; 
    } 
}; 

int main(){ 
    A a = 0; 
} 

나에게 오류를 제공합니다 출력은 단지

입니다.

그래서 내가 아는 한 컴파일러는 생성자를 요청하지만 사용하지 않습니다. 왜? 이것은 나에게 의미가 없습니다.

P. 나는

A a = 0; 

A a = A(0); 

만의 것과 동일합니다 이유도 이동 생성자도 이동 할당 연산자가 호출되어 있다고 가정?

+0

를? – Brotcrunsher

+2

이것은 C++ 17에서 변경되었습니다 (삭제 된 이동 생성자는 지금 컴파일됩니다). 그러나 copy/move elision은 항상 그 전에 문제였습니다. – chris

+0

g ++ 4.9, g ++ 6.3 및 clang 5.0에서 모두 시도했습니다. (-O0 -std = C++ 11) –

답변

3

C++ 표준에 따르면 (12.8 복사 및 이동 클래스 객체)를 사용하는 컴파일러

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies): .... — when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

30 A program is ill-formed if the copy/move constructor or the copy/move assignment operator for an object is implicitly odr-used and the special member function is not accessible (Clause 11). [ Note: Copying/moving one object into another using the copy/move constructor or the copy/move assignment operator does not change the layout or size of either object. —end note ]

+0

표준을 이해하는 것이 매우 어렵다는 것을 알고 있습니다. 설명이나 샘플 코드를 제공해 줄 수 있습니까? –

+0

@HảiPhạmLê 굵게 표시된 텍스트를 읽으면됩니다. 그리고 샘플 코드가 질문에 주어집니다. –

관련 문제