質問

私はstd::unique_ptr<>を含むクラスを持っていると私はstd::map<>のこのクラスの内部のインスタンスを載せていきたいと思います。私は(ベクターの場合には、それが実際に動作する)C ++に移動セマンティクスの導入の動機というものの一つは、標準コンテナ内unique_ptrsのようなものを置くの可能性だと思いました。しかし、std::map<>はこのアイデア好きではないように私には思えます。なぜこれがあるので、?

#include <map>
#include <memory>

int main()
{
    std::map<int,std::unique_ptr<int>> map;

    // error on the line that follows (use of disabled lvalue copy constructor)
    map.insert(std::make_pair(1,std::unique_ptr<int>(new int(2))));

    return 0;
}

感謝します。

- 編集

だけをより明確にするために、正確なエラーメッセージがあります:

mingw32-g++.exe --std=gnu++0x   -ID:\CodeEnv\Libraries\Boost  -c D:\CodeEnv\CodeMess\Untitled1.cpp -o D:\CodeEnv\CodeMess\Untitled1.o
mingw32-g++.exe  -o D:\CodeEnv\CodeMess\Untitled1.exe D:\CodeEnv\CodeMess\Untitled1.o   
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_algobase.h:66,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h:62,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/map:60,
                 from D:\CodeEnv\CodeMess\Untitled1.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/unique_ptr.h: In copy constructor 'std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >::pair(const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&)':
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_pair.h:68:   instantiated from 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&, _Val = std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/ext/new_allocator.h:111:   instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, _Args&& ...) [with _Args = const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&, _Tp = std::_Rb_tree_node<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h:394:   instantiated from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&, _Key = int, _Val = std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >, _KeyOfValue = std::_Select1st<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h:881:   instantiated from 'std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = int, _Val = std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >, _KeyOfValue = std::_Select1st<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h:1177:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = int, _Val = std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >, _KeyOfValue = std::_Select1st<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_map.h:500:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = int, _Tp = std::unique_ptr<int, std::default_delete<int> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::unique_ptr<int, std::default_delete<int> > > >]'
D:\CodeEnv\CodeMess\Untitled1.cpp:7:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/unique_ptr.h:214: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>]'
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_pair.h:68: error: used here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/map:60,
                 from D:\CodeEnv\CodeMess\Untitled1.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h: In constructor 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&, _Val = std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >]':
c:\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/stl_tree.h:136: note: synthesized method 'std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >::pair(const std::pair<const int, std::unique_ptr<int, std::default_delete<int> > >&)' first required here 

それはstd::pair<>に問題だが、単独で使用する場合、それは完全に正常に動作しますようにそれはなります:

int main()
{
    std::pair<int,std::unique_ptr<int>> pair;

    pair = std::make_pair(1,std::unique_ptr<int>(new int(2))); // no errors
    return 0;
}

それは明らかにそれが悪用されることができないという意味ではありませんが。

int main()
{
    std::pair<int,std::unique_ptr<int>> pair1,pair2;

    pair1 = std::make_pair(1,std::unique_ptr<int>(new int(2))); // no errors
    pair2 = pair1; // BOOM ! Copy from lvalue
    return 0;
}
おそらく内部std::map<>の何が起こるかです。

手がかり?

- 編集

エラーメッセージの確認、本当にTDM-GCC 4.4.1のstd::map<>の実装に問題があることを起こります。 std::vector<>::push_back(value_type&&)のような動きの意味を意識した挿入方法を特徴としていないようです。

今何をすべきか?

役に立ちましたか?

解決

私は同じ問題に遭遇しました。私はビット/ stl_map.hおよびビット/ stl_tree.hにパッチを当てることで、あまりにも多くの努力なしに必要な機能を追加することができました。仕事へのあなたの特定の例を取得するには、基本的にはちょうどマップクラスに挿入(VALUE_TYPE &&)メソッドを追加する必要があります。それは本質的に(CONST VALUE_TYPE&)インサートと同じであるが、()適切な場合はstd ::移動を使用して。あなたにも、右サポートメソッドを追加する必要がありますが、あなたは必要とされている何を伝えるために、コンパイルエラーを使用することができます。

この問題は、GCCチームに報告されています(ます。http:// GCC .gnu.org / bugzillaを/ show_bug.cgi?ID = 44436こちら)と誰かがそれに取り組んでいるので、うまくいけば、我々はすぐに連想し、順不同の容器に移動constructiblesのサポートが表示されます。

他のヒント

コンパイラは、あなたは何を使用していますか?これは、VS10ベータ2で罰金をコンパイルします。

<時間>

編集の問題のコンパイラはGCC

GCCは、あなたがこの問題を修正するパッチを提出することができ、オープンソースであるため、これが表示されますが、STLの実装にかかわる問題であること。
それはあまりにも難しいことではありませんので、幸いにも修正はSTLのコードではなく、コンパイラのコードである。

それまでの間は、ローカルpair / mapのヘッダファイルで、あなたの修正を入力することができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top