更新:回避策(元の意図)にAutoAを使用するコード例を編集しました。 rlbondの答えを見て、これを実現しました。std :: auto_ptr Visual Studio 6.0でのコンパイルの問題
は、私はこのスレッドからの勧告に基づいて、私のコードでauto_ptr
の使用を組み込むしようとしています:のVisual Studio 6.0でコンパイルするとき
Express the usage of C++ arguments through method interfaces
しかし、私はいくつかの予期しないコンパイルエラーを受け付けております。派生型のstd::auto_ptr
の代入/コピーを基底型のstd::auto_ptr
に処理するときに問題があります。これは私のコンパイラ特有の問題ですか?
Boostを使用することを強くお勧めしますが、私のプロジェクトではオプションではありません。 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はまだベータ版です。 –