업데이트 : 해결 방법으로 AutoA를 사용하는 편집 된 코드 예제 (원래의 의도 임). rlbond의 대답을보고 나서 이것을 깨달았습니다. std :: auto_ptr Visual Studio 6.0의 컴파일 문제
나는이 스레드의 추천에 따라 내 코드에auto_ptr
의 사용을 통합하기 위해 노력하고 있어요 :
Express the usage of C++ arguments through method interfaces
을 그러나, 나는 비주얼 스튜디오 6.0로 컴파일 할 때 예기치 않은 오류가 컴파일 수신하고 있습니다. 파생 형식의 std::auto_ptr
의 할당/복사본을 기본 형식 인 std::auto_ptr
에 적용 할 때 문제가 있습니다. 이 문제는 제 컴파일러에 관련된 것입니까?
부스트를 사용하는 것이 좋습니다.하지만 내 프로젝트에서는 옵션이 아닙니다. auto_ptr
을 계속 사용하려면 std::auto_ptr::release()
으로 전화를 걸어 해결 방법을 사용해야합니까? 지금까지 내가 겪은 것으로부터이 문제는 컴파일러 오류가 발생하므로 쉽게 잡을 수 있습니다. 그러나 기본 형식의 'auto_ptr'에 할당하기 위해 릴리스를 호출하는 컨벤션을 채택하여 유지 관리 문제에 대해 알려줄 수 있습니까? 특히 다른 컴파일러로 빌드 된 경우 (다른 컴파일러에는이 문제가 없다고 가정).
내 상황으로 인해 release()
해결 방법이 좋지 않은 경우 소유권 이전을 설명하기 위해 다른 규칙을 사용하여 대체해야합니까?
다음은 문제를 설명하는 예입니다.
#include "stdafx.h"
#include <memory>
struct A
{
int x;
};
struct B : public A
{
int y;
};
typedef std::auto_ptr<A> AutoA;
typedef std::auto_ptr<B> AutoB;
void sink(AutoA a)
{
//Some Code....
}
int main(int argc, char* argv[])
{
//Raws to auto ptr
AutoA a_raw_to_a_auto(new A());
AutoB b_raw_to_b_auto(new B());
AutoA b_raw_to_a_auto(new B());
//autos to same type autos
AutoA a_auto_to_a_auto(a_raw_to_a_auto);
AutoB b_auto_to_b_auto(b_raw_to_b_auto);
//raw derive to auto base
AutoB b_auto(new B());
//auto derive to auto base
AutoA b_auto_to_a_auto(b_auto); //fails to compile
//workaround to avoid compile error.
AutoB b_workaround(new B());
AutoA b_auto_to_a_auto_workaround(b_workaround.release());
sink(a_raw_to_a_auto);
sink(b_raw_to_b_auto); //fails to compile
return 0;
}
컴파일 오류 :
Compiling...
Sandbox.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Sandbox\Sandbox.cpp(40) : error C2664: '__thiscall std::auto_ptr<struct A>::std::auto_ptr<struct A>(struct A *)' : cannot convert parameter 1 from 'class std::auto_ptr<struct B>' to 'struct A *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Program Files\Microsoft Visual Studio\MyProjects\Sandbox\Sandbox.cpp(47) : error C2664: 'sink' : cannot convert parameter 1 from 'class std::auto_ptr<struct B>' to 'class std::auto_ptr<struct A>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
Sandbox.exe - 2 error(s), 0 warning(s)
심각하게? 11 년 된 컴파일러? 사람들이 계속 나아갈 것 같아. – shoosh
이동할 곳이있을 것이라고 생각할 것입니다. 10은 새로운 6이지만 10은 여전히 베타 버전입니다. –